[英]Python and AJAX - HTTP Response
I'm trying to answer an ajax-request with a python script. 我正在尝试用python脚本回答ajax请求。 The ajax-request sends a json-object just like follows:
ajax-request发送一个json-object,如下所示:
$.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: 我的Python脚本mycgi.py具有以下几行:
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.
我的浏览器获取响应,并识别-如果状态设置为“ 200 OK”-请求成功,并在成功部分中使用返回的对象运行命令。
But I have two questions: 但是我有两个问题:
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. 与将状态代码设置为200的响应类似,您可以在返回其他状态代码时向响应添加内容。 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 .
根据状态码,您应该在内容中添加详细信息和/或添加其他标题,以防您遵循标准的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. 在JavaScript中,您可以为来自ajax调用的响应的特定状态代码或处理过程中的所有错误设置处理程序。 See http://api.jquery.com/jQuery.ajax/ for details.
有关详细信息,请参见http://api.jquery.com/jQuery.ajax/ 。 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. 如果您想返回HTML并显示它,只需将响应转换为jQuery对象。
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:
只是一个简短的清单,事情在我的头上:
So in your case I would even go a bit further and would take a look at a slim Python web-application frameworks. 因此,在您的情况下,我什至会更进一步,并介绍一个苗条的Python Web应用程序框架。 But this very much depends on your application of course.
但这当然取决于您的应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.