jQuery的AJAX 200的状态,但神秘的语法错误 [英] jQuery AJAX 200 status, yet mysterious syntax error

查看:238
本文介绍了jQuery的AJAX 200的状态,但神秘的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个状态200所,当我使用jQuery的AJAX返回。不过,我也得到一个语法错误的地方。我张贴到PHP这样的:

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

我的PHP code是pretty的简单:

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:

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

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

一切正常。这是什么错误?哪里会这样&LT; 出现在错误可能来自

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

感谢

推荐答案

这是你的错误处理程序被炒鱿鱼和原木:

It is your error handler that gets fired and logs:

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

注意 $ AJAX 数据类型:。JSON 将触发错误处理程序,即使服务器返回 200 OK 但响应无效JSON。语法错误是不是在你的JavaScript code,但在JSON。确定其中&LT; 是从哪里来的,并确保你的PHP脚本发送有效的JSON

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.

提示:打开控制台,并期待在网络选项卡;所有XHRs随同标题和正文记录在那里。

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

这篇关于jQuery的AJAX 200的状态,但神秘的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆