我一直在阅读JSONP并试图获得有效的实现。下面的代码表示我对使用jQuery看起来应该是什么的理解。由于某种原因,我不能让这个工作,我不知道为什么。我在网上看过很多示例脚本,但出于某种原因,它们都不适合我。有人可以帮忙吗?我这样做了吗?
这是JSONP脚本:
<script type="text/javascript">
$(document).ready(function() {
var url = "http://www.example.com/blog/?json=get_recent_posts";
$.getJSON(url + "?callback=?", function(data) {
$("#output_div").append("<p>" + data.posts[2].title + "</p>");
}
});
});
</script>
...我写了一个像这样的div:
<div id="output_div"> </div>
谢谢!
答案 0 :(得分:1)
由于callback
是第二个参数,您需要使用&
将其附加到url + "&callback=?"
或
$(document).ready(function () {
var url = "http://www.example.com/blog/?json=get_recent_posts&callback=?";
$.getJSON(url, function (data) {
$("#output_div").append("<p>" + data.posts[2].title + "</p>");
});
});