2

I am trying to do some coding, further manipulations, blah blah and finally get json object and return a Jsonp(json object wrapped in javascript function)

I managed to create till json object which is

 js_on = {"name": "xxx","job":"unemployed", "remarks": "enjoying_life"}.

I am stuck at doing JSONP part. The tutorials I read are pretty confusing and demands many lines of coding. Is it really that complicated or is there any simpler way to do that? my code for JSONP starts with the following.

 callback = request.GET.get('callback')
 if callback:
 ......
 ......
 return(HttpResponse(...,content_type ='application/javascript'))

On server side I am using Python and Django framework. Thanks.

lante
  • 7,192
  • 4
  • 37
  • 57
Anna
  • 21
  • 1
  • 4

1 Answers1

5

Imagine a GET request with this format:

request: 'some/url?callback=myFunction'

In some kind of pseudo code, JSONP on server side should be:

response = '{ "somejson": "someValue" }'

if (request.params.callback != null)
    response = request.params.callback + '(' + response + ')'

So the data retrieved to the client will be: myFunction({ "somejson": "someValue" }).

Then just call it from client-side this way:

script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'some/url?callback=myFunction';

See also this question.

Update

You clarify that you are using Django, so maybe this blogpost may help you. It explains how to return a JSONP with a decorator. Also you should take a look at the Django documentation about renderers.

Community
  • 1
  • 1
lante
  • 7,192
  • 4
  • 37
  • 57
  • client side is okay. I have problem on server side of converting json object to json wrapped in js function. js_on = {"name": "xxx","job":"unemployed", "remarks": "enjoying_life"} to myFunction({"name":"xxx","job":"unemployed","remarks":"enjoying_life")) I am trying long time for that pseudocode part that you mentioned.How do I approach with that? Thanks. – Anna Dec 15 '14 at 15:42
  • just append the name of the function to the string you are returning. what are you using on server side? – lante Dec 15 '14 at 15:46
  • i mean, which language – lante Dec 15 '14 at 15:49
  • ca_l = myFunction({js_on}); response(HttpResponse(ca_l),content_type('application/javascript')) I tried some ways like one above and figured out calling the myfunction does not serve the purpose because i need to script down the required JSONP format in server side. Hope this clears what I am lookin for? – Anna Dec 15 '14 at 15:55
  • python and django framework – Anna Dec 15 '14 at 15:56
  • thats a bit different, I edited your questing and added that you are using Django and Python. I dont know about that language, but googling a bit I found [this blogpost](https://coderwall.com/p/k8vb_a/returning-json-jsonp-from-a-django-view-with-a-little-decorator-help) that may help you. – lante Dec 15 '14 at 16:01
  • JSONP Response with Django as follows: data = json.dumps({"something":"something"}) data = '%s(%s);' % (request.REQUEST['callback'], data) return HttpResponse(data, "text/javascript") – elad silver Jan 18 '16 at 12:26