Jquery $。ajax响应为空

Jquery $。ajax响应为空,jquery,asynchronous,Jquery,Asynchronous,我看到了其他相关的问题,但找不到与我的问题完全匹配的答案。正在尝试使用$.ajax从worldweatheronline.com获取JSON,并继续获得空响应。然而,在Fiddler中,我每次都看到一个正确的响应,只有在浏览器中,它似乎是空的(使用Firebug和alert语句查找)。当响应可用时,使用.ajax方法解析JSON 下面是代码片段、原始请求和原始响应 var jsonxhr = $.ajax({ url: 'http://free.w

我看到了其他相关的问题,但找不到与我的问题完全匹配的答案。正在尝试使用$.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");
                }
            }
        });
var jsonxhr=$.ajax({
网址:'http://free.worldweatheronline.com/feed/weather.ashx?q=55122&format=json&num_of_days=5&key=',
键入:“GET”,
数据类型:“jsonp”,
成功:函数(json){
警报(“请求成功”);
},
错误:函数(xhr,状态){
警报(“请求错误”);
},
完成:功能(xhr,状态){
//警报(“请求完成”);
试一试{
var parsedJson=$.parseJSON(jsonxhr.responseText);
//警报(“成功解析”);
警报('test'+jsonxhr.responseText+'test');
警报('headers'+jsonxhr.getAllResponseHeaders());
//警报(parsedJson.data.weather.tempMaxF.toString());
}
捕获(e){
警报(“错误解析”);
}
}
});
原始请求

获取HTTP/1.1 接受:/ 接受语言:en US 接受编码:gzip,deflate 用户代理: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;.NET4.0C;.NET4.0E;Tablet PC 2.0) 连接:保持活力 主持人:free.worldweatheronline.com

原始响应

HTTP/1.1200ok 缓存控制:专用 内容类型:application/json;字符集=utf-8 服务器:Microsoft IIS/7.5 X-AspNet-Version:2.0.50727 X-Powered-By:ASP.NET 日期:2011年12月28日星期三04:20:46 GMT 内容长度:2712


{“数据”:{“当前状况”:[{“云量”:“100”,“湿度”:“50”,“观测时间”:“03:50 AM”,“精度”:“0.0”,“压力”:“1017”,“温度C”:“-3”,“温度F”:“26”,“能见度”:“16”,“天气代码”:“122”,“天气描述”:[{“值”:“阴天”}],“天气指数”:[{“值”:"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png“}],“winddir16Point”:“N”,“winddirDegree”:“10”,“windspeedKmph”:“11”,“windspeedKmph”:“7”}],“请求”:[{“查询”:“55122”,“类型”:“Zipcode”}],“天气”:[{“日期”:“2011-12-27”,“精度”:“0.0”,“tempMaxC”:“2”,“tempMaxF”:“35”tempMinC:“-4”,“tempMinF:”24”,“weatherCode:”113”,“weatherDesc:[{“值”:“Sunny”}],“weathericonur:[{“值”:”http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png“}],“winddir16Point”:“NNW”,“winddirDegree”:“343”,“winddirection”:“NNW”,“windspeedKmph”:“28”,“windspeedMiles”:“18”},{“date”:”2011年12月28日,“precipMM”:“0.0”,“tempMaxC”:“2”,“tempMaxF”:“36”,“tempMinC”:“-6”,“tempMinF”:“22”,“weatherCode”:“113”,“weatherDesc”:[{“value”:“Sunny”}],“weathericonur”:[{“value”:”http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png“}],“winddir16Point”:“SW”,“winddirDegree”:“220”,“winddirection”“:”SW“,”windspeedKmph“:”17“,”Windspeeddmiles“:”11“,”{”日期“:”2011-12-29“,”precipMM“:”0.3“,”tempMaxC“:”4“,”tempMaxF“:”39“,”tempMinC“:”1“,”tempMinF“:”30“,”天气代码“:”116“,”weatherDesc“:”[{”数值“:”部分多云“,”天气数值“:”"http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0002_sunny_intervals.png“}],“winddir16Point”:“WSW”,“winddirDegree”:“257”,“winddirection”:“WSW”,“windspeedKmph”:“20”,“windspeedKmph”:“13”},{“日期”:“2011-12-30”,“precipMM”:“0.3”,“tempMaxC”:“1”,“tempMaxF”:“35”,“tempMinC”:“-1”,“tempMinF”:“30”,“天气代码”:”119,“weatherDesc”:[{“值”:“多云”}],“weatherIconUrl”:[{“值”:”http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0003_white_cloud.png“}],“winddir16Point”:“SW”,“winddirDegree”:“228”,“winddirection”:“SW”,“windspeedKmph”:“18”,“windspeedKmph”:“11”},{“date”:“2011-12-31”,“precipMM”:“0.7”,“tempMaxC”:”4、“tempMaxF”:“39”、“tempMinC”:“0”、“tempMinF”:“32”、“weatherCode”:“122”、“weatherDesc”:[{“value”:“cloud”}]、“weatherIconUrl”:[{“value”:”http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0004_black_low_cloud.png“}],“winddir16Point”:“W”,“winddirDegree”:“268”,“winddirection”:“W”,“windspeedKmph”:“21”风速表“:“13”}]}

我认为问题在于
jsonp
不是真正的AJAX。正因为如此,
xhr
对象并没有在真正的AJAX调用中看到的所有内容。你应该使用传递给
success
回调的数据。我同意。传递到success回调的数据包含解析了json,所以您不需要使用
$.parseJSON()
。谢谢各位,这确实解决了问题,我能够解析。我不明白您所说的“jsonp”其实不是AJAX“,@James Montagne。我认为jsonp也是AJAX,但用于域间调用。。。现在我不知道为什么,但我的请求只在数据类型为“jsonp”时有效,而在尝试“json”时失败。如果您能对此有所了解,我们将不胜感激。请阅读。JSONP使用脚本绕过AJAX不能进行跨域调用的事实。它缺少AJAX中的X,没有涉及XML。未使用XMLHttpRequest。