0

I've been reading up on JSONP and trying to get a working implementation. The code below represents my understanding of what this is supposed to look like using jQuery. For some reason I can't get this working, and I don't know why. I've seen many example scripts online, but for some reason, none of them work for me. Can anyone help? Am I doing this correctly?

Here's the JSONP script:

<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>

... and I wrote a div like this:

<div id="output_div"> </div>

Thanks!

4

1 回答 1

1

Since callback is the second parameter you need to use & to append it to the url like url + "&callback=?" or

$(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>");
    });
});
于 2013-11-15T00:07:29.313 回答