jQuery - 无法解析 json

jQuery - Cannot parse json

我正在 json 通过我的 Python 服务器发送响应,使用 json.dumps

return '{"status":200, "response":%s}' % (json.dumps(data))

我正在按以下方式通过 jQuery 检索它。

$.ajax({
        dataType: "jsonp",
        type : "GET",
        url: url,
        data : {},
        success : function(result){
            alert(result)
        },
        error: function (jqXHR, textStatus, errorThrown) {
            alert(jqXHR)
            alert(textStatus)
            alert(errorThrown)
        },
    });

我得到的回应是

{
    "status": 200,
    "response": {
        "count": 2,
        "2015_03_16": [
            {
                "imei1": "111",
                "status": "Success",
                "imei2": "NA",
                "msisdn": "111",
                "offer_id": "1",
                "ctype": "pre",
                "date": "2015-03-16 06:42:46",
                "recharge_api": "12",
                "points": "2",
                "operator": "vodafone",
                "model": "MicromaxA069",
                "circle": "delhi"
            },
            {
                "imei1": "111",
                "status": "Success",
                "imei2": "NA",
                "msisdn": "111",
                "offer_id": "1",
                "ctype": "pre",
                "date": "2015-03-16 06:42:46",
                "recharge_api": "12",
                "points": "2",
                "operator": "vodafone",
                "model": "MicromaxA069",
                "circle": "delhi"
            }
        ]
    }
}

在提示错误时,它显示 parserror。我读过各种 post 需要在 json 中发送数据以便 jquery 解析它,我也做了同样的事情。为什么我仍然收到错误消息?

编辑

我刚刚创建了一个虚拟代码,

<html>
    <body>
        Helo
    </body>
    <script type="text/javascript" src="untitled.js"></script>
    <script type="text/javascript">
    $(function(){
        $.ajax({
            type : "GET",
            url: 'a.json',
            data : {},
            success : function(result){
                alert(result)
            },
            error: function (jqXHR, textStatus, errorThrown) {
                console.log(jqXHR)
            },
        });
    });

    </script>
</html>

我在同一目录下创建了一个文件a.json,内容如下。

{
    "status": 200,
    "response": {
        "count": 3,
        "2015_03_16": [
            {
                "InstallCount": "1",
                "ad": "Amazon",
                "conversion": null,
                "ClickCount": "0"
            },
            {
                "InstallCount": "2",
                "ad": "Mobikwik",
                "conversion": "0.6667",
                "ClickCount": "3"
            },
            {
                "InstallCount": "1",
                "ad": "Quickr",
                "conversion": "1.0000",
                "ClickCount": "1"
            }
        ]
    }
}

当我运行上面的HTML代码时,它仍然进入错误函数并记录以下结果。

readyState 4
responseText "{"status":200, "response":{"count": 3, "2015_03_16": [{"InstallCount": "1", "ad": "Amazon", "conversion": null, "ClickCount": "0"}, {"InstallCount": "2", "ad": "Mobikwik", "conversion": "0.6667", "ClickCount": "3"}, {"InstallCount": "1", "ad": "Quickr", "conversion": "1.0000", "ClickCount": "1"}]}} "
status 200
statusText "OK"
abort function(a)
always function()
complete function()
done function()
error function()
fail function()
getAllResponseHeaders function()
getResponseHeader function(a)
overrideMimeType function(a)
pipe function()
progress function()
promise function(a)
setRequestHeader function(a, b)
state function()
statusCode function(a)
success function()
then function()

我现在哪里错了你也可以在你的系统中试试上面的代码

您告诉 jQuery 期望 JSONP,而不是 JSON。 JSONP JSON 包装在回调中,但您没有提供此类回调包装。

$.ajax() 调用的 jsonp 类型中删除 p,或添加回调包装器。回调名称可在 callback GET 参数中找到。

您还需要 return 具有正确内容类型的适当 JSON 响应。由于您使用的是 Flask,因此 最简单 的方法是使用 jsonify() function;它将创建一个响应 object,其中包含为您设置的正确内容类型:

from flask import jsonify

# ...

return jsonify(status=200, response=data)

另一种方法是 return 您构建的字符串,但带有状态代码和 header 字典:

return '{"status":200, "response":%s}' % (json.dumps(data)), 200, {'Content-Type': 'application/json'}

无论哪种方式,您都可以使用 the decorator from this Flask snippet 支持 JSONP。你会把它放在 @app.route() 装饰器之后,你的视图函数之前:

@app.route('/some/path')
@jsonp
def your_view_function():
    # ...

服务器return错误

您可以使用 Chrome 开发人员工具

查找错误

打开开发者工具,点击"Console"

res = [{"status":200, "response":{"count": 2, "2015_03_16": [{"imei1": "111 ", "status": "Success", "imei2": "NA", "msisdn": "111", "offer_id": "1", "ctype": "pre", "date": "2015-03-16 06:42:46", "recharge_api": "12", "points": "2", "operator": "vodafone", "model": "MicromaxA069", "circle": "delhi"}, {"imei1": "111", "status" : "Success", "imei2": "NA", "msisdn": "111", "offer_id": "1", "ctype": "pre" , "date": "2015-03-16 06:42:46", "recharge_api": "12", "points": "2", "operator": "vodafone", "model": "MicromaxA069", "circle": "delhi"}]}}

您将收到错误信息:Uncaught SyntaxError: Unexpected token }

很容易找到响应结尾需要一个“]”

抱歉我的英语不好