Return to Snippet

Revision: 14417
at July 24, 2009 16:57 by ndnichols


Updated Code
import simplejson
def jsonp(func):
    def foo(self, *args, **kwargs):
        callback, _ = None, None
        if 'callback' in kwargs and '_' in kwargs:
            callback, _ = kwargs['callback'], kwargs['_']
            del kwargs['callback'], kwargs['_']
        ret = func(self, *args, **kwargs)
        if callback is not None:
            ret = '%s(%s)' % (callback, simplejson.dumps(ret))
        return ret
    return foo   

class Server(object):
    @cherrypy.expose
    @jsonp
    def MyMethod(self, arg1):
        #do something with arg1
        return 'Works'

Revision: 14416
at June 3, 2009 16:46 by ndnichols


Updated Code
import simplejson
def jsonp(func):
    def foo(self, callback, _, *args, **kwargs):
        ret = func(self, *args, **kwargs)
        ret = '%s(%s)' % (callback, simplejson.dumps(ret))
        return ret
    return foo

class Server(object):
    @cherrypy.expose
    @jsonp
    def MyMethod(self, arg1):
        #do something with arg1
        return 'Works'

Revision: 14415
at June 3, 2009 16:45 by ndnichols


Initial Code
import simplejson
def jsonp(func):
    def foo(self, callback, _, *args, **kwargs):
        ret = func(self, *args, **kwargs)
        ret = '%s(%s)' % (callback, simplejson.dumps(ret))
        return ret
    return foo

Initial URL

                                

Initial Description
This is a @property to take care of jsonp stuff if you're using CherryPy.
In your Javascript, (you're using JQuery, right?) you can do
`
$.ajax({url:"localhost:8080/MyMethod", data:{'arg1':'foo'}, dataType:"jsonp", success:onMyMethodComplete});
`
and you will get back your JS datastructure without having to worry about the jsonp stuff yourself.

Initial Title
JSONP property for cherrypy

Initial Tags
jquery

Initial Language
Python