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

当我使用jQuery AJAX时,我返回了状态200。 但是,我也从某处获得语法错误。 我发布到这样的PHP:

function submit_order(orderInformation) { $.ajax({ type: 'post', url: 'queries/submit_order.php?', 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?', returnedData, function (el, data, context) { console.log('success'); $('#content_container').fadeIn(340); }); }); }, error: function (xhr, ajaxOptions, thrownError) { console.log(xhr.status); console.log(thrownError); } }); } 

我的PHP代码非常简单:

 $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); 

然而我明白了:

错误截图

奇怪的是,如果我将来自console.log(JSON.stringify(orderInformation))的JSON字符串复制粘贴到PHP页面中:

 $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}}}} '; 

一切正常。 这个错误是什么? 这个<在错误中看到的来自哪里?

谢谢

它是你的error handling程序被触发和日志:

  • xhr.status(200)
  • thrownError(语法错误)

请注意,带有dataType: json $.ajax将触发error handling程序,即使服务器返回200 OK但响应是无效的JSON。 语法错误不在JavaScript代码中,而是在JSON中。 确定<来自哪里,并确保您的PHP脚本发送有效的JSON。

提示:打开控制台并查看网络选项卡; 所有XHR都与标题和正文一起记录在那里。

200 – 服务器的回复是好的http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

您的响应服务器中出现语法错误,返回无效的json

由于您的PHP代码接缝很好,所以必须有其他东西。 语法错误或你的框架返回json包装的json …

使用适当的工具查看服务器返回的内容。 (关于chrome上firefox /开发人员工具的firebug)

在你的图像中你看到0: "<"这意味着返回的字符串以< - 开头,这意味着返回的是html。

看起来你使用chrome。 转到chrome中的“网络”标签,您应该能够看到针对您的请求的原始响应。

所以这是一个PHP错误:

$sector_index是不可以忍受的。 你可以var_dump看看。 这是什么?

看起来没有得到处理。 在发布之前提醒URL以进行validation。