9

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?

Jeff Tratner
  • 16,270
  • 4
  • 47
  • 67
Michael
  • 2,031
  • 6
  • 21
  • 27

4 Answers4

9

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!

Zach
  • 2,441
  • 1
  • 16
  • 20
6

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})
Michael
  • 2,031
  • 6
  • 21
  • 27
1

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.

mhawke
  • 84,695
  • 9
  • 117
  • 138
1

You forgot closing quote and semicolon in the first line =)

If it is not helps, check following:

  1. List item
  2. What are you using as python service? Django, flask, something else? Maybe you can provide provide python code?
  3. Also, look at the 'Net' Firebug's tab and, after ensure that request to 'url' is actually handled (not returned 404 or 50x codes, but 200), check 'Response' sub-tab.

Hope this helps!

Alex G.P.
  • 9,609
  • 6
  • 46
  • 81