我的JSON有问题。我相信我们正在从SOAP服务中获取它。我正在使用jQuery AJAX来尝试使用JSON。我们使用Asp。. NET解决方案,所以我知道"d"安全特性,并在我的dataFilter中有一种方法来尝试绕过它。我在Firebug中获得JSON,但不是到我的本地机器。该服务位于与我的本地计算机分开的域中。我知道跨域策略问题,但当我在jQuery AJAX调用中将数据类型作为"jsonp"时,似乎正在消耗Firebug中的JSON。如果我将我的dataType设置为"json",我在Firebug中什么也得不到。
问题是我的JSON中有""斜杠。我想这就是为什么Firebug给我一个"无效标签"错误。但我不确定。
- 我如何过滤掉""而不做服务器端代码。
- 我怎样才能在我的页面上得到JSON和警报。
我的jQuery Ajax调用如下
$(document).ready(function () {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
async: false,
url: "http://www.myexternaldomain.com/jservice/myservice.svc/CurrentJsonDataFullWeatherPanel",
data: "{}",
dataType: "jsonp",
dataFilter: function(data) {
// This boils the response string down
// into a proper JavaScript Object().
var msg = eval('(' + data + ')');
if (msg.hasOwnProperty('d'))
return msg.d;
else
return msg;
},
success: function(msg) {
// This will now output the same thing
// across any current version of .NET.
alert(msg);
},
error: function (result) {
alert('Failed: ' + result.status + ' ' + result.statusText);
}
});
});
我在Firebug中的JSON输出如下所示:{"d":"({"AirTemperature ","57.3 ","BarometricPressure ","30.08 ","CurrentRainRate ":""," 30.7 DewPointTemperature ","","HeatIndex ": " 57.3 ","HourlyRainAmount ": " 0.00 ","LocalObservationTime ": " 10/14/2011 11:16:07是","MonthlyRainAmount ":""," 36 RelativeHumidity ","","SnowDepth ": " 0.0 ","SolarRadiation ":""," 日出","7:09日落","","22点","TodayRainAmount ": " 0.00 ","能见度":"6561 ","风寒指数 ": " 57.3 ","WindDirection ":"2 ","WindGust 11.4":"","WindGustDirection ":"92 ","":"4.9风速","YearlyRainAmount 22.24":"","StationTime ": " 10/14/2011 11:15:24是"}"}
有什么建议吗?我是否需要使用不同于jQuery的方式来使用JSON?
我应该补充说,我的失败警报是发送200成功,但击中错误。
非常感谢帮助。
将eval('(' + data + ')');
替换为$.parseJSON(data)
文档:http://api.jquery.com/jQuery.parseJSON/