如何使用jsonp或jquery.getjson获取第三方域的json响应

How to get json response of third party domain using jsonp or jquery.getjson

本文关键字:第三方 json 响应 获取 getjson 何使用 jsonp jquery      更新时间:2023-09-26

我是新手。我想在javascript中得到第三方域名的json响应。所以,我尝试了Ajax http请求,但它只是为同一域。所以,我正在尝试使用json和jquery.getjson.Still我没有得到任何结果,请帮助我。

代码:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>get json in alert</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script type="text/javascript">
$.getJSON("http://cricketapi.mblogi.com/sfsjson.php?callback=?", function(result){
   //response data are now in the result variable
   alert(result);
});
</script>
</head>
<body>

</body>
</html> 

您需要确保您正在使用的URL以json格式返回数据。

例如,更新代码以访问Flickr提要:

$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?ids=8623220@N02&format=json&jsoncallback=?", function(result){
   //response data are now in the result variable
   alert(result);
});

这里的键在URL的末尾,&format=json&jsoncallback=?,它告诉Flickr服务器发回json数据。

所以你的解决方案是更新URL