您能给出一个用jquery读取jsonp请求的非常简单的例子吗?我就是不能让它工作。
发布于 2010-04-22 04:42:17
以下是工作示例:
<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=?
。这向getJSON
函数表明我们想要使用JSONP。删除它,将使用普通的JSON请求。这将由于same origin policy而失败。
您可以在JQuery站点上找到更多信息和示例:http://api.jquery.com/jQuery.getJSON/
https://stackoverflow.com/questions/2681466
复制相似问题