Javascript jQueryAJAX200状态,但有神秘的语法错误
当我使用jQueryAjax时,会返回状态200。然而,我也从某处得到了一个语法错误。我在PHP上的帖子如下:Javascript jQueryAJAX200状态,但有神秘的语法错误,javascript,php,jquery,ajax,json,Javascript,Php,Jquery,Ajax,Json,当我使用jQueryAjax时,会返回状态200。然而,我也从某处得到了一个语法错误。我在PHP上的帖子如下: function submit_order(orderInformation) { $.ajax({ type: 'post', url: 'queries/submit_order.php?<?=time();?>', data: 'orderInformation=' + JSON.stringify(orderI
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);
}
});
}
但我明白了:
奇怪的是,如果我将JSON字符串从console.log(JSON.stringify(orderInformation))
复制粘贴到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}}}} ';
一切正常。这个错误是什么?这个
200-是服务器的正常响应吗http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html
响应服务器返回无效json时出现语法错误
由于您的PHP代码很好,所以肯定还有其他东西。语法错误或框架返回html包装的json
使用适当的工具查看服务器返回的内容。(firefox上的firebug/chrome上的开发者工具)
在您的图像中,您会看到0:“看起来
未得到处理。在发布URL以进行验证之前,请提醒该URL。是您的错误处理程序触发并记录:
- xhr.status(200)
- thrownError(语法错误)
请注意,$.ajax
withdataType:json
将启动错误处理程序,即使服务器返回200 OK
,但响应是无效的json。语法错误不在JavaScript代码中,而是在json中。请确定200不是错误的位置。这是来自Web服务器的OK响应。是否尝试打开 在浏览器中查询/submit_order.php?
或使用firebug检查服务器返回的确切内容?看起来您有语法错误或其他问题,html是错误的returned@Codeguy007那么,为什么要在错误
回调时将其打印到日志中呢?谢谢!肯定不行,因为您无法运行php客户端。ch也是检查服务器返回的是什么?原始响应。谢谢。我发现语法错误来自php打印出的
警告:为foreach()提供的参数无效在第54行的/public\u html/bch/querys/submit\u order.php中
因此,这是一个php问题:)您可以将其作为另一个问题发布,或者忍者编辑您的问题。PS:$order\u information
很可能不是数组。php正在向字符串添加斜线:/stripslashes
修复了它。它总是d吗o这是什么?谢谢你的帮助!现在检查一下$sector_索引中的内容。由于php不知道如何遍历它,php出于某种原因在它接收的JSON字符串中添加了斜杠。我不知道它是否总是这样做,但stripslashes似乎已经修复了它。谢谢你给我讲解:)
$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}}}} ';