admin管理员组

文章数量:1180511

I have this code:

<script src=".7.2/jquery.min.js"></script>
<script>
    $(document).ready(function() {
        $.getJSON('', function(fbResults) {
            document.write(fbResults.cats[0].title);
        });
    });
</script>

How can I change this code:

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

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

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?

Share Improve this question edited Mar 19, 2016 at 12:35 rrk 15.8k4 gold badges30 silver badges47 bronze badges asked Aug 11, 2012 at 18:53 Satch3000Satch3000 49.4k89 gold badges224 silver badges349 bronze badges 3
  • Is the data offered from the server as JSONP? – user1106925 Commented Aug 11, 2012 at 19:07
  • There's just JSON data on the server – Satch3000 Commented Aug 11, 2012 at 19:17
  • 3 Then it won't work as a JSONP request. JSONP is basically just a <script> request. The response must have the data wrapped in a function call, like my_func({"foo":"bar"}). Then when the script arrives, assuming there's a function named my_func, that function is invoked passing the data into it. Some servers let you specify the function name, and they'll return the JSONP response, but this behavior must be established on the server. – user1106925 Commented Aug 11, 2012 at 19:25
Add a comment  | 

1 Answer 1

Reset to default 35

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);
    });
});

本文标签: javascriptChanging getJSON to JSONPStack Overflow