2011-12-28 86 views
0

我已经看到其他相关问题,但无法找到与我的问题完全匹配的SO。尝试使用$ .ajax从worldweatheronline.com获取JSON,并一直获得空响应。然而,在Fiddler中,我每次看到正确的响应,只有在浏览器中,它似乎是空的(使用Firebug和alert语句查找)。使用.ajax方法,因为我想在响应可用时解析JSON。

下面是代码片段,原始请求和原始响应。

  var jsonxhr = $.ajax({ 
      url: 'http://free.worldweatheronline.com/feed/weather.ashx?q=55122&format=json&num_of_days=5&key=<somekey>', 
      type: 'GET', 
      dataType: 'jsonp', 
      success: function (json) { 
       alert('request success'); 
      }, 
      error: function (xhr, status) { 
       alert('request error'); 
      }, 
      complete: function (xhr, status) { 
       //alert('request complete'); 
       try { 
        var parsedJson = $.parseJSON(jsonxhr.responseText); 
        //alert('success parsing'); 
        alert('test' + jsonxhr.responseText + 'test'); 
        alert('headers ' + jsonxhr.getAllResponseHeaders()); 
        //alert(parsedJson.data.weather.tempMaxF.toString()); 
       } 
       catch (e) { 
        alert("error parsing"); 
       } 
      } 
     }); 

原始请求

GET http://free.worldweatheronline.com/feed/weather.ashx?q=55122&format=json&num_of_days=5&key=somekey HTTP/1.1 接受:/ 接受语言:EN-US 接受编码:gzip,紧缩 的User-Agent:Mozilla的/4.0(兼容; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET 4.0C; NET4.0E; Tablet PC 2.0) 连接:K EEP-活着 主机:free.worldweatheronline.com

原始响应

HTTP/1.1 200 OK 缓存控制:私人 内容类型:应用/ JSON; x-AspNet-Version:2.0.50727 X-Powered-by:ASP.NET Date:Wed,28 Dec 2011 04:20:46 GMT Content- {“data”:{“current_condition”:[{“cloudcover”:“100”,“humidity”:“50”,“observation_time”:“03:50 AM”,“precipMM”):2712

“0.0”,“压力”:“1017”,“temp_C”:“-3”,“temp_F”:“26”,“可见度”:“16”,“weatherCode”:“122”,“weatherDesc” {“value”:“Overcast”}],“weatherIconUrl”:[{“value”:“http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png”}],“winddir16Point”:“N” ,“winddirDegree”:“10”,“windspeedKmph”:“11”,“windspeedMiles”:“7”}],“request”:[{“query”:“55122”,“type”:“Zipcode”}] ,“天气”:[{“date”:“2011- 12mm“,”precipitationMM“:”0.0“,”tempMaxC“:”2“,”tempMaxF“:”35“,”tempMinC“:”-4“,”tempMinF“:”24“,”weatherCode“ “113”,“weatherDesc”:[{“value”:“Sunny”}],“weatherIconUrl”:[{“value”:“http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png”} ],“winddir16Point”:“NNW”,“winddirDegree”:“343”,“winddirection”:“NNW”,“windspeedKmph”:“28”,“windspeedMiles”:“18”},{“date” -12-28“,”precipitationMM“:”0.0“,”tempMaxC“:”2“,”tempMaxF“:”36“,”tempMinC“:”-6“,”tempMinF“:”22“ :“113”,“weatherDesc”:[{“value”:“Sunny”}],“weatherIconUrl”:[{“value”:“http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png” }“winddir16Point”:“SW”,“winddirDegree”:“220”,“winddirection”:“SW”,“windspeedKmph”:“17”,“windspeedMiles”:“11”},{“date” “tempMaxC”:“4”,“tempMaxF”:“39”,“tempMinC”:“-1”,“tempMinF”:“30”,“weatherCode” “:”116“,”weatherDesc“:[{”value“:”部分多云“}],”w “winddir16Point”:“WSW”,“winddirDegree”:“257”,“winddirection”,“风向标”,“风向标” :“WSW”,“windspeedKmph”:“20”,“windspeedMiles”:“13”},{“date”:“2011-12-30”,“precipMM”:“0.3”,“tempMaxC” ,“tempMaxF”:“35”,“tempMinC”:“-1”,“tempMinF”:“30”,“weatherCode”:“119”,“weatherDesc”:[{“value”:“Cloudy”}] “weatherIconUrl”:[{“value”:“http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png”}],“winddir16Point”:“SW”,“winddirDegree”:“228”,“winddirection “:”SW“,”windspeedKmph“:”18“,”windspeedMiles“:”11“},{”date“:”2011-12-31“,”precipMM“:”0。“tempMaxC”:“4”,“tempMaxF”:“39”,“tempMinC”:“0”,“tempMinF”:“32”,“weatherCode”:“122”,“weatherDesc”:[{“值“:”Overcast“}],”weatherIconUrl“:[{”value“:”http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png“}],”winddir16Point“:”W“,” winddirDegree“:”268“,”winddirection“:”W“,”windspeedKmph“:”21“,”windspeedMiles“:”13“}]}}

回答

2

我相信问题是jsonp不是真的AJAX 。因此,xhr对象没有您在真正的AJAX调用中可能会看到的所有内容,您应该使用传递给success回调的数据来代替

+2

我同意。成功回调包含解析的json,所以你不会ne编辑使用'$ .parseJSON()'。 – 2011-12-28 04:42:21

+0

谢谢你们,它确实解决了它,我能够解析。我不明白,你的意思是“jsonp不是真正的AJAX”,@James Montagne。我认为jsonp也是AJAX,但用于域间调用......现在我不知道为什么,但我的请求只适用于当我有'jsonp'数据类型,并且当我尝试'json'时失败。任何关于此的灯都将非常感激。 – strider 2011-12-28 20:01:32

+0

阅读http://en.wikipedia.org/wiki/JSONP。 JSONP使用脚本来解决AJAX无法进行跨域调用的事实。它在AJAX中缺少X,没有涉及XML。不使用XMLHttpRequest。 – 2011-12-29 02:15:31