Python and AJAX - HTTP response

I am trying to respond to an ajax request using a python script. Ajax request sends a json object as shown below:

$.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)) 

In principle, everything is working fine. My browser receives a response, recognizes - if the status is set to "200 OK" - that the request was successful and runs commands in terms of success with the returned object.

But I have two questions:

  • How do I return an error script with an error message? I can return β€œ404” instead of β€œ202 OK,” and my browser understands that there is an error. But I have no idea how to return an error message. Should I put this message in the header of my reply?
  • I would like to know if my way of communicating between the client and server is β€œtoo simple” and perhaps too risky. When I read other questions with similar problems, I saw that people are working with python modules such as a "request" or getting a class from BaseHTTPRequestHandler. I would like to simplify my python scripts as much as possible. This - for some reason - maybe you need to go the other way?

Thanks a lot!

+4
source share
1 answer

To your first question:

Like your answers, when you set the status code to 200, you can add content to the response when returning other status codes. Depending on the status code, you should add content specifics 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 configure handlers for specific status codes for the response you receive from your ajax call, or for all errors as you do it. See http://api.jquery.com/jQuery.ajax/ for more details. In these handlers, you access the contents of the response and display a 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); } } 

If you want to return the HTML and render it, just include the response in the jQuery object.

 var response = $(xhr.responseText); 

To your second question:

I personally prefer to use existing tools rather than run parts of the lowest-level application. There is a huge list of advantages that, in my opinion, are when using libraries that hide unnecessary complexity and template. Just a very short list of things at the top of my head:

  • I do not want to reinvent the wheel.
  • Less bugs when using libraries that are used by thousands of other developers
  • My own code improves when using high-level abstractions
  • Good examples on the Internet when using well-known libraries
  • Future needs are often already covered.

So, in your case, I would even go a little further and look at the thin framework of Python web applications. But it really depends on your application, of course.

+2
source

All Articles