jQuery.ajax()parsererror [英] jQuery.ajax() parsererror

查看:205
本文介绍了jQuery.ajax()parsererror的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试从 http://api-v3.deezer.com/1.0/search/album/?q=beethoven&index=2&nb_items=2&output=json with:

when i try to get JSON from http://api-v3.deezer.com/1.0/search/album/?q=beethoven&index=2&nb_items=2&output=json with:

(jQuery 1.6.2)

$.ajax({
    type: "GET",
    url: url,
    dataType: "jsonp",
    success: function (result) {
        alert("SUCCESS!!!");
    },
    error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.statusText);
        alert(xhr.responseText);
        alert(xhr.status);
        alert(thrownError);
    }
});

我得到: parsererror; 200;不确定的; jquery162 ********************未被调用

但是使用JSON来自 http://search.twitter.com/search.json?q= beethoven& callback =?& count = 5 工作正常。
两者都是有效的JSON格式。那么这个错误是什么?

but with the JSON from http://search.twitter.com/search.json?q=beethoven&callback=?&count=5 works fine. Both are valid JSON formats. So what is this error about?

[更新]

@ 3ngima,我已经在asp.net中实现了它,它运行正常:

@3ngima, i have implemented this in asp.net, it works fine:

$.ajax({
    type: "POST",
    url: "WebService.asmx/GetTestData",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (result) {
        alert(result.d);
    }
});

WebService.asmx:

WebService.asmx:

[WebMethod]
public string GetTestData()
{
    try
    {
        var req = System.Net.HttpWebRequest.Create("http://api-v3.deezer.com/1.0/search/album/?q=beethoven&index=2&nb_items=2&output=json");
        using (var resp = req.GetResponse())
        using (var stream = resp.GetResponseStream())
        using (var reader = new System.IO.StreamReader(stream))
        return reader.ReadToEnd();
    }
    catch (Exception) { return null; }
}


推荐答案

这是因为你是告诉jQuery你期待 JSON-P ,而不是 JSON ,回来了。但回报是JSON。 JSON-P名字错误,以一种不会导致混淆的方式命名。它是约定,用于通过脚本标记将数据传递给函数。相比之下,JSON是一种数据格式。

It's because you're telling jQuery that you're expecting JSON-P, not JSON, back. But the return is JSON. JSON-P is horribly mis-named, named in a way that causes no end of confusion. It's a convention for conveying data to a function via a script tag. In contrast, JSON is a data format.

JSON示例:

{"foo": "bar"}

JSON-P示例:

yourCallback({"foo": "bar"});

JSON-P有效,因为JSON是JavaScript文字符号的子集。 JSON-P只不过是一个承诺,如果你告诉服务你正在调用哪个函数名称来回调(通常在请求中放入一个 callback 参数),响应将采用 functionname(data)的形式,其中 data 将为JSON(或更常见) ,一个JavaScript文字,可能不是相当相同的东西)。你打算在脚本标签的 src (jQuery为你做的)中使用JSON-P URL,绕过同源政策,以防止Ajax请求从源自其原始文件的来源请求数据(除非服务器支持 CORS ,您的浏览器也支持。)

JSON-P works because JSON is a subset of JavaScript literal notation. JSON-P is nothing more than a promise that if you tell the service you're calling what function name to call back (usually by putting a callback parameter in the request), the response will be in the form of functionname(data), where data will be "JSON" (or more usually, a JavaScript literal, which may not be the quite the same thing). You're meant to use a JSON-P URL in a script tag's src (which jQuery does for you), to get around the Same Origin Policy which prevents ajax requests from requesting data from origins other than the document they originate in (unless the server supports CORS and your browser does as well).

这篇关于jQuery.ajax()parsererror的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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