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) {
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.
source share