简体   繁体   English

Python和AJAX-HTTP响应

[英]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: 但是我有两个问题:

  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. 我可以返回“ 404”而不是“ 202 OK”,并且我的浏览器意识到存在错误。 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. 当我在这里阅读其他类似问题时,我看到人们使用python模块(例如“ request”)或从BaseHTTPRequestHandler派生一个类。 I would like to keep my python-scripts as simple as possible. 我想让我的python脚本尽可能简单。 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. 与将状态代码设置为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: 只是一个简短的清单,事情在我的头上:

  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. 因此,在您的情况下,我什至会更进一步,并介绍一个苗条的Python Web应用程序框架。 But this very much depends on your application of course. 但这当然取决于您的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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