jsonp

104 views
Skip to first unread message

shlomo bauer

unread,
Mar 6, 2013, 2:44:40 PM3/6/13
to we...@googlegroups.com
I need to have one of my webpy services return jsonp

serializing into a json string is easy enough.

I can't wrap the response in the jquery callback value - and I have no clue how to get the value.

I can't find any documentation.

Help.

thanks

Pradeep Banavara

unread,
Mar 6, 2013, 10:41:10 PM3/6/13
to we...@googlegroups.com
I haven't done this myself yet but it's as simple as wrapping the serialized JSON in an object, In python I don't believe there is a concept of a 'callback'. jQuery's callback = a python object. I may be wrong though.

-pradeep


--
You received this message because you are subscribed to the Google Groups "web.py" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webpy+un...@googlegroups.com.
To post to this group, send email to we...@googlegroups.com.
Visit this group at http://groups.google.com/group/webpy?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

anh le

unread,
Mar 7, 2013, 12:03:52 AM3/7/13
to we...@googlegroups.com
Hello,

I think return jsonp is something like this:
url: .../somepage?callback=bar

class SomePage:
    def GET(self):
        callback = web.input(callback='default_callbackname') 
        web.header('Content-type', 'application/javascript')
        data = json.dumps({'a': 'foo'}) ' # or call some func that return serialized data
        content = str(callback) + '(' + data  + ')' 
        return content

Just the quick outline I haven't test this yet.


Shannon Cruey

unread,
Mar 7, 2013, 4:29:10 PM3/7/13
to we...@googlegroups.com
My function is too long to paste here, but this is the (untested) relevant lines.  I have jsonp callback working with jquery ajax.

def GET(self, method):
        args = web.input()
        web.header('Content-Type', 'application/json')
        payload = json.dumps({'a': 'foo'}) ' # or call some func that return serialized data
        return "%s('%s')" % (args["callback"], payload)

I can't find my jquery example, but basically it executes the command you got back.  callbackfunc(jsondata);

Anand Chitipothu

unread,
Mar 7, 2013, 10:04:24 PM3/7/13
to webpy
On Fri, Mar 8, 2013 at 2:59 AM, Shannon Cruey
<shanno...@cloudsidekick.com> wrote:
> My function is too long to paste here, but this is the (untested) relevant
> lines. I have jsonp callback working with jquery ajax.
>
> def GET(self, method):
> args = web.input()
> web.header('Content-Type', 'application/json')
> payload = json.dumps({'a': 'foo'}) ' # or call some func that return
> serialized data
> return "%s('%s')" % (args["callback"], payload)
>
> I can't find my jquery example, but basically it executes the command you
> got back. callbackfunc(jsondata);

That is the typical pattern that I follow too.

json = simplejson.dumps(result)
web.header('Content-Type', 'text/javascript')
if i.callback:
return "%s(%s);" % (i.callback, json)
else:
return json

Here is a complete example.

https://github.com/internetarchive/openlibrary/blob/master/openlibrary/coverstore/code.py#L422

Anand
Reply all
Reply to author
Forward
0 new messages