微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

当我使用jQuery AJAX时,我返回了状态200.但是,我也从某个地方收到语法错误.我发布到这样的PHP

function submit_order(order@R_223_4045@ion) {
    $.ajax({
        type: 'post',
        url: 'queries/submit_order.PHP?<?=time();?>',
        data: 'order@R_223_4045@ion=' + JSON.stringify(order@R_223_4045@ion),
        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);
      }
    });
}

我的PHP代码非常简单:

$order_@R_223_4045@ion = 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_@R_223_4045@ion);

但我得到了这个:

奇怪的是,如果我将来自console.log(JSON.stringify(order@R_223_4045@ion))的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}}}} ';

一切正常.这个错误是什么?哪个<看到错误来自? 谢谢

解决方法:

它是你的错误处理程序被触发和日志:

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

请注意,带有dataType:json的$.ajax将触发错误处理程序,即使服务器返回200 OK但响应是无效的JSON.语法错误不在JavaScript代码中,而是在JSON中.确定<来自并确保您的PHP脚本发送有效的JSON. 提示:打开控制台并查看网络选项卡;所有XHR都与标题和正文一起记录在那里.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐