JSON callback handling function in jQuery

I am new to jQuery and maybe this is question n00b. And also my English is not the best.

I wrote a service in my Google App Engine application that delivers data in JSON format, which works fine, but I was not able to parse the JSON data using jQuery:


var url= 'myapp.appspot.com/myservice.json?someparams';
$.getJSON(url, function(json){
    alert("Success parsing JSON");  // I never reached this code 
    ....
});

After days of reading posts and tutorials, I felt this slide show: http://www.slideshare.net/andymckay/cross-domain-webmashups-with-jquery-and-google-app-engine

While reading slide 23, I noticed a "callback =?" and I tried the code in slide 42:


class MyJSONHandler(webapp.RequestHandler):
    def get(self):
        ## Retrieve some data from DB or MemCached
        jsonData = json.dumps(data)
        if self.request.get('callback'):
            self.response.out.write('%s(%s)' % (self.request.get('callback'), jsonData))
        else:
            self.response.out.write(jsonData)

And in the jQuery function:


$.getJSON(url+'&callback=?', function(json){
    alert("Success parsing JSON");  // Now i'm here !!
    ....
});

My question is:

" " ? : "?" ( "MyJSON": [{ "a-lot": "of-data" }]) '??

.

+3
4

callback JSONP.

jQuery getJSON <script>, URL, .
, URL- , callback, .

AJAX, JSONP .

+2

AJAX, . , script src, URL-, script , , .

: http://en.wikipedia.org/wiki/JSON#JSONP

+1

It is called JSONP.

You can look here: What is JSONP?

Hope this helps

0
source

This technique is called JSONP: JSON with Padding and is used to bypass a policy of the same origin .

0
source

Source: https://habr.com/ru/post/1793087/


All Articles