Changing getJSON to JSONP

24,915

Actually, you just have to add ?callback=?, jQuery does the rest.

$(document).ready(function() {
    $.getJSON('http://example.com/api/get_cats?callback=?', function(fbResults) {
        document.write(fbResults.cats[0].title);
    });
});
Share:
24,915
Satch3000
Author by

Satch3000

Updated on March 20, 2020

Comments

  • Satch3000
    Satch3000 about 4 years

    I have this code:

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <script>
        $(document).ready(function() {
            $.getJSON('http://example.com/api/get_cats', function(fbResults) {
                document.write(fbResults.cats[0].title);
            });
        });
    </script>
    

    How can I change this code:

    <script>
        $(document).ready(function() {
            $.getJSON('http://example.com/api/get_cats', function(fbResults) {
                document.write(fbResults.cats[0].title);
            });
        });
    </script>
    

    for it to work as JSONP ... Is this totally different?