dongshuang0011 2012-12-13 18:23
浏览 41
已采纳

jQuery AJAX 200状态,但神秘的语法错误

I have a status 200 being returned when I'm using jQuery AJAX. However, I am also getting a syntax error from somewhere. I am posting to PHP like this:

function submit_order(orderInformation) {
    $.ajax({
        type: 'post',
        url: 'queries/submit_order.php?<?=time();?>',
        data: 'orderInformation=' + JSON.stringify(orderInformation),
        dataType: 'json',
        success: function (returnedData) {
            console.log(returnedData);
            $('#content_container').fadeOut(340, function () {
                var new_content = $('#content_container').clone(false);
                $('#content_container').remove();
                new_content.css('display', 'none');
                new_content.children().remove();

                new_content.appendTo('body');
                $('#content_container').vkTemplate('templates/confirm_template.tmpl?<?=time()?>', returnedData, function (el, data, context) {
                console.log('success'); 

                    $('#content_container').fadeIn(340);
                });
            });
        },
      error: function (xhr, ajaxOptions, thrownError) {
        console.log(xhr.status);
        console.log(thrownError);
      }
    });
}

My PHP code is pretty straightforward:

$order_information = json_decode($json_str, true);

//go through the array and make an email out of it
//add a few elements to the array
//send the email

//send back a json string with the added elements
echo json_encode($order_information);

Yet I get this:

error screenshot

And oddly, if I copy paste the JSON string from console.log(JSON.stringify(orderInformation)) into the PHP page:

$json_str = '{"sector_0":{"file":[],"sector_info":{"sector_label":"NIO","purchase_order":"test","proof":false},"lines":{"line_0":{"description":"test","quantity":"2","productId":"1","addressId":"20","shipViaId":"1","notes":false}}}} ';

everything works. What is this error? Where could this < seen in the error be coming from?

Thanks

  • 写回答

3条回答 默认 最新

  • dongxia9620 2012-12-13 18:46
    关注

    It is your error handler that gets fired and logs:

    • xhr.status (200)
    • thrownError (the syntax error)

    Note that $.ajax with dataType: json will fire the error handler even if the server returns 200 OK but the response is invalid JSON. The syntax error is not in your JavaScript code but in the JSON. Identify where the < is coming from and make sure that your PHP script is sending valid JSON.

    Tip: open the console and look at the network tab; all XHRs are logged there along with headers and body.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog