简体   繁体   中英

Python and AJAX - HTTP Response

I'm trying to answer an ajax-request with a python script. The ajax-request sends a json-object just like follows:

$.ajax({
     url : "cgi-bin/mycgi.py",
     type : "POST",
     data : JSON.stringify(MyJSONObject),
     success : function() {...do something},
     error : function(xhr,errmsg,err) {
           alert(xhr.status);
     }
}

My Python script mycgi.py has the following lines:

import sys
import simplejson as json

...

myjson = json.loads(sys.stdin.read())

#do something with the object

mystatus = "200 OK"

sys.stdout.write("Status: %s\n" % mystatus)
sys.stdout.write("Content-Type: application/json")
sys.stdout.write("\n\n")
sys.stdout.write(json.dumps(myjson))

Basically everything works fine. My browser gets the response, recognizes - if status is set to "200 OK" - that the request was successfull and runs the commands in the success-part with the returned object.

But I have two questions:

  1. How can I get my script to return errors with an error-Message? I can return "404" instead of "202 OK" and my browser realizes, that there is an error. But I have no good idea how to return an error-Message. Do I have to put this message in the header of my response?
  2. I would like to know, if my way of communication between client and server is "too simple" and perhaps too riskful. When i read other questions here with similar problems, I saw that people work with python-modules like "request" or derive a class from BaseHTTPRequestHandler. I would like to keep my python-scripts as simple as possible. Is it - out of some reason - perhaps necessary to go another way?

Thanks a lot!

To your first question:

Similar to your responses where you set the status code to 200, you can add content to the response when you return other status codes. Depending on the status code you should add specifics to the content and/or add additional headers in case you want to follow the standard http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html .

In JavaScript you can setup handlers for specific status codes for the response you get from your ajax call, or for all errors as you do it. See http://api.jquery.com/jQuery.ajax/ for details. In those handlers you have access to the response content and display the message:

$.ajax({
   url : "cgi-bin/mycgi.py",
   type : "POST",
   data : JSON.stringify(MyJSONObject),
   success : function() {...do something},
   error : function(xhr,errmsg,err) {
       // show status code and response content
       alert(xhr.status + ": " + xhr.responseText);
   }
}

In case you want to return HTML and display it, just turn the response into an jQuery object.

var response = $(xhr.responseText);

To your second question:

I personally prefer to use existing tools rather than doing the low level parts of an application all myself. There is a huge list of benefits I think one has when using libraries that hide unnecessary complexity and boilerplate. Just a very short list with things at the top of my head:

  1. I don't want to re-invent the wheel
  2. Less bugs when using libraries which are used by thousands of other developers
  3. My own code improves when using high level abstractions
  4. Good examples on the internet when using well known libraries
  5. Future needs often already covered

So in your case I would even go a bit further and would take a look at a slim Python web-application frameworks. But this very much depends on your application of course.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM