使用JQuery JSON调用失败

时间:2011-09-28 14:31:43

标签: jquery json

我一直在搜索arround,我无法从这个示例中检索JSON信息。请有人帮帮我吗?

var jsonURL = "http://mdc2.cbuc.cat/dmwebservices/index.php?q=dmGetCollectionList/json";

var jqxhr = $.getJSON(jsonURL, function(data) {
  alert("Success!");
  alert(data[0].alias); 
})
.success(function() { alert("second success"); })
.error(function() { alert("error"); })
.complete(function() { alert("complete"); });

jqxhr.complete(function(){ alert("second complete"); });

var jqxhr = $.getJSON(jsonURL, function(data) { alert("Success!"); alert(data[0].alias); }) .success(function() { alert("second success"); }) .error(function() { alert("error"); }) .complete(function() { alert("complete"); }); jqxhr.complete(function(){ alert("second complete"); });

我已经检查了网址,并且它说的是有效且格式良好的所有地方......

2 个答案:

答案 0 :(得分:1)

该代码是否在mdc2.cbuc.cat上托管该代码?否则,您可能会遇到same origin policy

答案 1 :(得分:1)

由于该请求没有返回正确的jsonp,浏览器无法解释它。

如果您有权访问该服务器,则需要修改它以接受回调函数,例如?callback=cbfunc,然后将json响应包装在回调函数中,例如cbfunc(["foo","bar"]);

如果您无权访问该服务器,则可以使用第三方解决方案(如YQL),也可以构建服务器端代理,以便为您提出请求。对于YQL,这是一个可以提供帮助的页面:

http://developer.yahoo.com/yql/console/#h=SELECT%20*%20FROM%20json%20WHERE%20url%3D%22http%3A//mdc2.cbuc.cat/dmwebservices/index.php%3Fq%3DdmGetCollectionList/json%22

选择json单选按钮,然后在页面底部找到一个url。只需删除callback=cbfunc部分。

这是生成的网址:

http://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20json%20WHERE%20url%3D%22http%3A%2F%2Fmdc2.cbuc.cat%2Fdmwebservices%2Findex.php%3Fq%3DdmGetCollectionList%2Fjson%22&format=json&callback=cbfunc

如果请求包含任何敏感数据,我建议不要使用YQL并使用服务器端脚本来获取数据。