jatin-practice / django-rest-interface

Automatically exported from code.google.com/p/django-rest-interface
0 stars 0 forks source link

jsonp support #39

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
In order to do cross domain javascript requests in the browser security
model, you need to wrap the output of json in a function call. This is
commonly called jsonp.

I would recommend to add an extra parameter &callback which text would wrap
the json response. For example, if 

http://example.com/api/something

returned

{"a":"b"}

then

http://example.com/api/something?callback=cbfunc

returns

cbfunc({"a":"b"})

Original issue reported on code.google.com by ptar...@gmail.com on 17 Jun 2009 at 8:57

GoogleCodeExporter commented 8 years ago
Proposed Patch

class JSONPResponder(JSONResponder) :
    def addCallback(self, request, response):
        try :
            cb = request.GET['callback']
            response.content = cb + "(" + response.content + ")"
        except KeyError :
            pass
        return response

    def element(self, request, *args, **kw):
        response = super(JSONPResponder, self).element(request, *args, **kw)
        return self.addCallback(request, response)

    def list(self, request, *args, **kw):
        response = super(JSONPResponder, self).list(request, *args, **kw)
        return self.addCallback(request, response)

Original comment by ptar...@gmail.com on 17 Jun 2009 at 9:21