回调 - 通过jQuery AJAX Parseerror JSONP [英] Callback - Parseerror JSONP via jQuery AJAX

查看:174
本文介绍了回调 - 通过jQuery AJAX Parseerror JSONP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑以下code片断:

  $(函数(){
     $阿贾克斯({
        键入:GET,
        网址:http://mosaicco.force.com/siteforce/activeretailaccounts
        数据类型:JSONP
        跨域:真正的,
        JSONP:回调,
        错误:函数(jqXHR,textStatus,errorThrown){
            警报('错误消息:'+ textStatus);
            警报('HTTP错误:'+ errorThrown);
            },
        成功:功能(数据){
                变种I = 0;
                       //处理JSON
                       $每个(数据,函数(){
                             VAR名称= this.locname;
                             VAR纬度= this.lat;
                             VAR LNG = this.lng;
                             VAR地址= this.address;
                             VAR地址2 = this.address2;
                             VAR城市= this.city;
                             VAR状态= this.state;
                             VAR邮政= this.postal;
                             VAR手机= this.phone;
                             VAR网络= this.web;
                             幅= web.replace(HTTP://,);

                             VAR距离=地理codeCalc.CalcDistance(orig_lat,orig_lng,纬度,经度,地理codeCalc.EarthRadiusInMiles);

                             //创建数组
                             locationset [我] =新的Array(距离,名称,纬度,经度,地址,地址2,城市,州,邮政,电话,网络);

                             我++;
                      });
                  }
        });
 });
 

我揪JSON跨域出于某种原因,我不断收到一个parseerror返回:

  HTTP错误:错误:jQuery17209875996995251626_1343943259805不叫
 

我能看到的数据就好在页面上 - 和回调看起来是这样的:

 回调:jQuery17209875996995251626_1343943259805_:1343943260015
 

请帮我诊断,我缺少的是什么 - !谢谢

解决方案

  VAR数据= $ .parseJSON(数据);
 

既然你正在做一个JSONP请求时,你会得到一个对象的文字传递给回调函数。上不能使用 parseJSON 。无论如何,jQuery是聪明,总是做解析为您如果内容类型是已知的。

不知道这是否会触发该错误消息,但对问题的<一个href="http://stackoverflow.com/questions/5095307/jquery-json-response-always-triggers-a-parseerror">jQuery JSON响应总是触发ParseError 这是解决方案。


确定,的简单。看脚本加载:那是没有的有效JSONP - 它错过了回调的填充的。此外,MIME类型是错误的:对于一个JSONP脚本,它应该是 文/ JavaScript的应用程序/ JavaScript的 ,为他们提供它应该是JSON的 应用程序/ JSON

jQuery也发现了脚本的负荷,但由于没有得到执行时,抛出一个错误说:给定的回调不叫,虽然该文件已成功加载。 - parseerror疑似

您确保Web服务支持JSONP呢?

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:

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. 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?

这篇关于回调 - 通过jQuery AJAX Parseerror JSONP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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