简体   繁体   English

回调 - 通过jQuery AJAX进行Parseerror JSONP

[英]Callback - Parseerror JSONP via jQuery AJAX

Please consider the following code snippet: 请考虑以下代码段:

$(function () {
     $.ajax({
        type: "GET",
        url: "http://mosaicco.force.com/siteforce/activeretailaccounts",
        dataType: "jsonp",
        crossDomain: true,
        jsonp: "callback",
        error: function(jqXHR, textStatus, errorThrown) {   
            alert('Error Message: '+textStatus);
            alert('HTTP Error: '+errorThrown);
            },
        success: function (data) {
                var i = 0;
                       //Process JSON
                       $.each(data, function () {
                             var name = this.locname;
                             var lat = this.lat;
                             var lng = this.lng;
                             var address = this.address;
                             var address2 = this.address2;
                             var city = this.city;
                             var state = this.state;
                             var postal = this.postal;
                             var phone = this.phone;
                             var web = this.web;
                             web = web.replace("http://", "");

                             var distance = GeoCodeCalc.CalcDistance(orig_lat, orig_lng, lat, lng, GeoCodeCalc.EarthRadiusInMiles);

                             //Create the array
                             locationset[i] = new Array(distance, name, lat, lng, address, address2, city, state, postal, phone, web);

                             i++;
                      });
                  }
        });
 });​

I am pulling JSON cross domain and for some reason I keep getting a parseerror returned: 我正在拉JSON跨域,并且由于某种原因我不断返回一个parseerror:

HTTP Error:Error: jQuery17209875996995251626_1343943259805 was not called

I am able to see the data just fine on the page - and the callback looks like this: 我能够在页面上看到数据很好 - 回调看起来像这样:

callback:jQuery17209875996995251626_1343943259805_:1343943260015

Please help me to diagnose what I am missing - thanks! 请帮我诊断我错过的东西 - 谢谢!

var data = $.parseJSON(data);

Since you are doing a JSONP request, you will get an object literal passed into the callback function. 由于您正在执行JSONP请求,因此您将获得一个传递给回调函数的对象文字。 You can't use parseJSON on that. 你不能在那上使用parseJSON Anyway, jQuery is intelligent and always does the parse for you if the content-type is known. 无论如何,jQuery是智能的,如果知道内容类型,它总是为你解析。

Not sure whether that triggers the error message, but to the question jQuery JSON response always triggers a ParseError this was the solution. 不确定是否会触发错误消息,但问题是jQuery JSON响应总是触发ParseError,这就是解决方案。


OK, that 's simple. 好的, 很简单。 Look at the script it loads : That is no valid JSONP - it misses the callback padding . 看看它加载脚本 :这不是有效的JSONP - 它错过了回调填充 Also, the mime-type is wrong: For a JSONP script, it should be text/javascript or application/javascript , for the JSON they deliver it should be application/json . 此外,mime类型是错误的:对于JSONP脚本,它应该是text/javascriptapplication/javascript ,对于他们提供的JSON应该是application/json

jQuery does detect the load of the "script", but as nothing gets executed it throws an the error that "the given callback was not called although the file was successfully loaded" - parseerror suspected. jQuery确实检测到“脚本”的负载,但是由于没有执行任何操作,它会抛出错误“尽管文件已成功加载但未调用给定的回调” - 怀疑是parseerror。

Are you sure that the webservice supports JSONP at all? 你确定web服务完全支持JSONP吗?

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

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