最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Callback - Parseerror JSONP via jQuery AJAX - Stack Overflow

programmeradmin3浏览0评论

Please consider the following code snippet:

$(function () {
     $.ajax({
        type: "GET",
        url: "",
        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:

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!

Please consider the following code snippet:

$(function () {
     $.ajax({
        type: "GET",
        url: "http://mosaicco.force./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:

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!

Share Improve this question edited Aug 3, 2012 at 15:20 McKev asked Aug 2, 2012 at 21:49 McKevMcKev 851 gold badge1 silver badge7 bronze badges 1
  • possible duplicate of jQuery JSON response always triggers a ParseError – Bergi Commented Aug 3, 2012 at 15:08
Add a ment  | 

1 Answer 1

Reset to default 4
var data = $.parseJSON(data);

Since you are doing a JSONP request, you will get an object literal passed into the callback function. You can't use parseJSON on that. Anyway, jQuery is intelligent and always does the parse for you if the content-type is known.

Not sure whether that triggers the error message, but to the question jQuery JSON response always triggers a ParseError this was the solution.


OK, that's simple. Look at the script it loads: That is no valid JSONP - it misses the callback padding. 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.

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.

Are you sure that the webservice supports JSONP at all?

发布评论

评论列表(0)

  1. 暂无评论