简体   繁体   English

Jquery JSONP 请求获得 200 响应数据但标记错误

[英]Jquery JSONP request gets 200 response with data but flags error

Got a simple autocomplete box (jquery ui) that gets its source from a web service.有一个简单的自动完成框(jquery ui),它从 web 服务获取其来源。 The code is something like below:代码如下所示:

var autocompleteOptions = {
    source = getDataFromService,
    minLength: 3
};

var getDataFromService = function(request, response) {
    var ajaxOptions = {
        url: "http://myservice:1234/somedata/",
        dataType: "jsonp",
        data: "someVariable = " + request.term,
        success: function(data) { alert("data"); },
        error: function(xhr, description, error) { alert("failed"); }
    };

    $.ajax(ajaxOptions);
}

$(someSelector).autocomplete(autocompleteOptions);

Looking in fiddler and even in the Firefox firebug panel, I can see that the JSON is correctly returned, and the server response is a 200. I have even checked the created jsonp script snippet, which also contains the correct JSON.查看提琴手,甚至在 Firefox 萤火虫面板中,我可以看到 JSON 正确返回,服务器响应是 200。我什至检查了创建的 jsonp 脚本片段,其中还包含正确的 Z0ECD11F81C1D23BBD8。 However it always hits the error function not the success one.但是,它总是遇到错误 function 而不是成功的错误。

I have also tried using complete and getting the data from the xhr manually, however the responseText and responseXml are both undefined.我也尝试过使用完整并手动从 xhr 获取数据,但是 responseText 和 responseXml 都未定义。 The error contained says parse error, but it all seems to be syntactically correct json, as the firebug panel and fiddler both display it fine.包含的错误说解析错误,但它似乎在语法上都是正确的 json,因为萤火虫面板和提琴手都显示它很好。

HTTP/1.1 200 OK
Server: ASP.NET Development Server/9.0.0.0
Date: 28 Jun 2011 11:17:04 GMT
X-AspNet-Version: 2.0.50727
X-AspNetMvc-Version: 2.0
Cache-Control: private
Content-Type: application/json; charset=utf-8
Content-Length: 29
Connection: Close

[{"id":"1", "somevar":"hello"}]

That JSON is not correct,那 JSON 不正确,

[{"id":"1", somevar:"hello"}]

needs to be需要是

[{"id":"1", "somevar":"hello"}]

JSON requires double quotes. JSON 需要双引号。

http://jsfiddle.net/robert/Y6ypV/ http://jsfiddle.net/robert/Y6ypV/

A value can be a string in double quotes, or a number, or true or false or null, or an object or an array.值可以是双引号中的字符串、数字、真或假、null、object 或数组。 These structures can be nested.这些结构可以嵌套。

Take From: http://www.json.org/取自: http://www.json.org/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM