Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSONP web service with python

Tags:

python

json

I'm writing an AJAX function that requests data from my JSON Python webservice. My AJAX request looks like:

  url = "http://localhost:8001/blah"
  $.ajax({
      url: url,
      type: 'get',
      dataType: 'jsonp',
      success: function(data) {
          console.log('hi')
      }
  });

For now, my python web service has a function that handles the request to '/blah' that has the following return statement:

return json.dumps({'a':1, 'b':2 })

My AJAX function is not successfully retrieving a response from my Python Webservice, but I don't get any errors in Firebug. What is my webservice or javascript doing wrong?

like image 531
Michael Avatar asked Jul 20 '12 07:07

Michael


People also ask

How do I call a webservice in Python?

The easiest way to GET or POST a response from a web service from Python is using requests. You do not note what version of python you are using so you may need to install requests using the command pip install requests. You also do not indicate if your web service requires authentication.

What are the Web services in Python?

Web services are XML-based information exchange systems that use the Internet for direct application-to-application interaction. These systems can include programs, objects, messages, or documents. A web service is a collection of open protocols and standards used for exchanging data between applications or systems.

Can JSONP execute JavaScript?

It was proposed by Bob Ippolito in 2005. JSONP enables sharing of data bypassing same-origin policy, which disallows running JavaScript code to read media DOM elements or XMLHttpRequest data fetched from outside the page's originating site.


3 Answers

Zack got it. My javascript was correct. I changed my python return statement to the following:

callback = request.args.get('callback')
return '{0}({1})'.format(callback, {'a':1, 'b':2})
like image 58
Michael Avatar answered Sep 30 '22 15:09

Michael


What happens when you use Jquery's JSONP datatype, is that a callback function name is sent as a GET param as part of your URL, so you're actually querying something like "http://localhost:8001/blah?callback=json125348274839".

Your response from your web server should look like this:

    return "%s({'a':1, 'b':2 })" % _GET_PARAMS('callback')

so your web server will return somthing like "json125348274839({'a':1, 'b':2 })"

Hope that helps!

like image 39
Zach Avatar answered Sep 30 '22 15:09

Zach


Turn on (or add) logging in your Python web service. Inspect your web server logs... are you receiving the request from your javascript client? Is your web server logging that it is returning a response?

Possibly it depends on the framework that you are using. Is it as simple as returning a string from the handler function? Perhaps the handler is supposed to return a list of strings and, because it is not getting a list, it is returning nothing to the client. Look in your web server logs for errors.

like image 40
mhawke Avatar answered Sep 30 '22 15:09

mhawke