小编典典

带有jQuery的jsonp

ajax

您能否举一个使用jquery读取jsonp请求的非常简单的示例?我就是无法正常工作。


阅读 368

收藏
2020-07-26

共1个答案

小编典典

这是工作示例:

<html><head><title>Twitter 2.0</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
</head><body>
<div id='tweet-list'></div>
<script type="text/javascript">
$(document).ready(function() {
    var url =  "http://api.twitter.com/1/statuses/user_timeline/codinghorror.json";
    $.getJSON(url + "?callback=?", null, function(tweets) {
        for(i in tweets) {
            tweet = tweets[i];
            $("#tweet-list").append(tweet.text + "<hr />");
        }
    });
});
</script>
</body></html>

注意所?callback=?请求URL的末尾。这表明getJSON我们要使用JSONP。删除它,将使用原始的JSON请求。由于相同的原产地政策而失败。

您可以在JQuery网站上找到更多信息和示例:http :
//api.jquery.com/jQuery.getJSON/

2020-07-26