【问题标题】:Invalid JSON Response?无效的 JSON 响应?
【发布时间】:2013-04-02 20:09:06
【问题描述】:

我有一个页面做一些事情,然后输出一个 JSON 响应。这个页面是从 jQuery Ajax 调用中调用的,但是当我在 Firefox 中检查帖子时,我得到了我不理解的 Invalid JSON。

这是页面的代码

Write("{status: 'ERROR', StatusCode: '" + result.StatusCode + "',payload: ''}");

输出看起来像这样(在我看来是正确的?)

{status: 'ERROR', StatusCode: '200',payload: ''}

这是我的 AJAX 调用

$.ajax({
            type:"POST",
            url: copyBUURL,
            data: { action: "copy_bu", bu_id: selected_bu.id, bu_name: selected_bu.name, new_parent: new_parent_bu.id, new_parent_name: new_parent_bu.name, new_name: $("#bu_name").val(), from_name: $("#bu_fromname").val(), email: $("#bu_email").val() },
            contentType:"application/x-www-form-urlencoded; charset=utf-8",
            dataType:"json",
            success:function(data){
                if(!data)
                {
                    alert("There was an error processing your request");
                    return false;
                }
                $("#createBtn").removeAttr("disabled");
                $("#cancelBtn").removeAttr("disabled");

                console.log("Data response: " + JSON.stringify(data));

                //xhr_users Landing Page
                showUrlInDialog('https://pages.umusic-mail.com//page.aspx?QS=472529ec60bdf32a5a46a47dceedf4ab0793800df7757ecbd2298ad0f8bc85eb&bu_id=' + data.bu_id + '&bu_name=' + data.bu_name);

                if (data.status == "OK") {

                    $("#msgBox").css("height", "80px");
                    $("#result").html("The Business Unit was successfully copied!<br /><br />Users will be assigned very shortly. (You will see a dialog window pop up in this page).");
                    $("#loader").attr("src", "https://dl.dropbox.com/u/417891/aeg-checkmark.png").css("display", "inline");
                    hideContainer();
                    resetForm();
                    $("#business_units").jstree("refresh");
                } else if (data.status.toUpperCase() == "ERROR") {
                    displayError(data.payload);
                    $("#msgBox").attr("class", "msgBoxOff");
                    $("#result").html("");
                    $("#loader").css("display", "none");
                } else {
                    // something way wrong
                }
            },
            error: function (xhr, ajaxOptions, thrownError) {
                console.log(xhr.status);
                console.log(thrownError);
            }
        });

【问题讨论】:

  • 错误是正确的,你的 JSON 无效。每个键都需要用双引号括起来,字符串也需要用双引号括起来。

标签: jquery ajax json post


【解决方案1】:

你需要在这样的所有内容上加上双引号:

{
    "status": "ERROR",
    "StatusCode": "200",
    "payload": ""
}

或者这个:

{
    "status": "ERROR",
    "StatusCode": 200,
    "payload": ""
}

如果您希望 StatusCode 为整数。
另外,如果您以后遇到问题,请考虑使用JSONLint 来验证您的 json。

【讨论】:

    猜你喜欢
    • 2015-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多