JSONP web service with python

后端 未结 4 1658
迷失自我
迷失自我 2020-12-16 04:26

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

  url = \"http://localhost:8001/blah\"
  $.aja         


        
相关标签:
4条回答
  • 2020-12-16 04:41

    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})
    
    0 讨论(0)
  • 2020-12-16 04:49

    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.

    0 讨论(0)
  • 2020-12-16 05:03

    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!

    0 讨论(0)
  • 2020-12-16 05:04

    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!

    0 讨论(0)
提交回复
热议问题