【发布时间】:2011-10-02 09:33:02
【问题描述】:
当我尝试从 http://api-v3.deezer.com/1.0/search/album/?q=beethoven&index=2&nb_items=2&output=json 获取 JSON 时:
(jQuery 1.6.2)
$.ajax({
type: "GET",
url: url,
dataType: "jsonp",
success: function (result) {
alert("SUCCESS!!!");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.statusText);
alert(xhr.responseText);
alert(xhr.status);
alert(thrownError);
}
});
我得到:parsererror; 200; undefined; jquery162******************** was not called
但是使用来自http://search.twitter.com/search.json?q=beethoven&callback=?&count=5 的 JSON 可以正常工作。 两者都是有效的 JSON 格式。那么这个错误是关于什么的呢?
[更新]
@3ngima,我已经在 asp.net 中实现了,效果很好:
$.ajax({
type: "POST",
url: "WebService.asmx/GetTestData",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (result) {
alert(result.d);
}
});
WebService.asmx:
[WebMethod]
public string GetTestData()
{
try
{
var req = System.Net.HttpWebRequest.Create("http://api-v3.deezer.com/1.0/search/album/?q=beethoven&index=2&nb_items=2&output=json");
using (var resp = req.GetResponse())
using (var stream = resp.GetResponseStream())
using (var reader = new System.IO.StreamReader(stream))
return reader.ReadToEnd();
}
catch (Exception) { return null; }
}
【问题讨论】:
-
您可以参考stackoverflow.com/a/29485687/3126639。 async: false 很重要。
标签: javascript jquery json parse-error