2

Beginning to climb the ajax learning curve, I'm trying to make a simple ajax call back to my CherryPy application and echo the data sent back to the browser.

My ajax call is working and I can return, for instance, the request method back to the browser.

I cannot, however, find the data sent by browser in the request object inside my CherryPy handler. Here is my CherryPy handler, cribbed from this question:

class Contact:

def index(self):

    cl = cherrypy.request.headers['Content-Length']
    rawbody = cherrypy.request.body.read(int(cl))
    body = None
    #body = simplejson.loads(rawbody)
    if body is None:
        return cherrypy.request.method + ' (no body found)'
    else:
        return cherrypy.request.method + ' ' + body


index.exposed = True

and here's my Javascript:

<script type="text/javascript">
function SendContactEntry() {
$.ajax( {type:        "POST",
     url:         "/contact/",
     data:        { word: "HELLO" },
     processData: false,
     cache:       false,
     contentType: "application/json",
     dataType:    "text",
     success:     function (response){
                alert(response);
              }
    }
);
}
</script>    

Using this code my browser receives back a response of "POST (no body found)".

What I want to do is learn, in my CherryPy handler, that I was sent a word value of "HELLO".

If I uncomment the line body = simplejson.loads(rawbody) I receive an HTML Status of 500 back from CherryPy. The same happens if I try to decorate my index() function with @cherrypy.tools.json_in().

Community
  • 1
  • 1
Larry Lustig
  • 49,320
  • 14
  • 110
  • 160

2 Answers2

1

word may be in cherrypy.request.params already. Check in there?

http://www.cherrypy.org/wiki/RequestObject#params

I don't think jQuery converts the stuff you put into data as a JSON object. http://api.jquery.com/jQuery.ajax/

Chris Cherry
  • 28,118
  • 6
  • 68
  • 71
  • Nothing in cherrypy.request.params (len() is 0). I'd be happy to find my value *anywhere* in the request — I've looked in the body, the headers, etc. – Larry Lustig Sep 28 '11 at 03:06
  • Can you sniff the request with the browser to see how its being sent? Inspector in Chrome, or Firebug in firefox. Also does `rawbody` contain any content? – Chris Cherry Sep 28 '11 at 03:12
  • I'm using the javascript console in Chrome to check the response codes, but I don't know how to view the outgoing request. – Larry Lustig Sep 28 '11 at 13:04
  • 1
    Network tab of the inspector, (open the tab then refresh the page to make sure its active) Then perform the action that triggers the AJAX request, you will see it show up as a new line in the Network tab, then click it and you'll have lots of information on the right: request, response, etc. – Chris Cherry Sep 28 '11 at 15:37
  • Thanks, I had no idea that I could spy things out to that level. In particular, my ajax call was receiving back the stack trace for the Python errors in those cases where I was just seeing Status 500 on the CherryPy console. Using that, and the call to JSON.stringify mentioned by Sean, along with the code from the earlier answer from fumanchu I now have JSON transfer working. – Larry Lustig Sep 29 '11 at 01:50
1

Since you have set processData to false and you are passing over an object you are sending over a stringified version of your data object - and there is nothing to see.

Either:

  1. Call JSON.stringify on your data object before sending it over (which should make your simplejson.loads call work).
  2. Remove your processData attribute and let jQuery send the request over as a normal URL-encoded request (and you can access your variables through request.params["word"]).
Sean Vieira
  • 155,703
  • 32
  • 311
  • 293
  • At this point, I'm not concerned with the data comes across as JSON or a query string. contentType is there because I found without it I received a failure status 500. I can't remember when and why I added the processData instruction, but I'll retest without that tonight. Just knowing (instead of only *expecting*) that the data should show up in `.params` is a help. – Larry Lustig Sep 28 '11 at 12:59
  • When I remove the processData argument, I still get no parameters in my server handler. If I also remove the contentType argument, my handler no longer executes, I get a status of 400. However, if I call JSON.stringify on my data, that works! – Larry Lustig Sep 29 '11 at 01:48