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!

7
  • 1
    Does the server your requesting the data from actually return JSONP, and it's not the same as regular JSON, as JSONP is wrapped in a function and works completely different from an ajax call. Commented Nov 15, 2013 at 0:08
  • No, to my knowledge, it returns regular JSON. I didn't know that it had to be output in JSONP as well. I haven't seen any mention about that in all my reading. I was of the understanding that JSONP is just a client-side implementation. Is that incorrect? Commented Nov 15, 2013 at 0:11
  • 1
    That is incorrect, JSONP gets around the cross-origin problem by inserting a script tag into the DOM, so it's not really an XMLHttpRequest, but it's neatly wrapped up like one in jQuery, and to get the data from the inserted script the JSON must be wrapped in a function, which would be the callback function, and jQuery creates a random name for that function and calls it when the script has loaded to get the content. In other words, to do a JSONP call, the server has to return valid JSONP, and not just JSON. Commented Nov 15, 2013 at 0:14
  • On the other hand, many API's support CORS, where this isn't an issue ? Commented Nov 15, 2013 at 0:15
  • 1
    The server either returns valid JSONP, it supports CORS, or it's not cross-origin. Commented Nov 15, 2013 at 0:43

1 Answer 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>");
    });
});
Sign up to request clarification or add additional context in comments.

4 Comments

That hasn't affected the results... Chrome is giving me the following syntax error: "Unexpected token }". What is that?
@shmuli there were an additional } in the code... fixed now
Thanks, that got it working! Now I can study this and learn this deeper. I appreciate your help!:)
I tried to click on the check and I accidentally hit the downvote. I removed the downvote and marked this as the correct answer. So the system should remove that downvote for you...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.