麻烦让SimpleHTTPRequestHandler响应AJAX [英] Having Trouble Getting SimpleHTTPRequestHandler to respond to AJAX

查看:148
本文介绍了麻烦让SimpleHTTPRequestHandler响应AJAX的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近尝试根据这个

虽然一切似乎都能从客户端收到请求,但我无法向客户端发送任何内容,当我尝试self.wfile.write(foo),我在客户端得到回复;但是,来自XmlObject的响应文本是完全空白的!?

Although everything seems to work as far as receiving the request from the client, I cannot send anything back to the client, when I try to self.wfile.write("foo"), I get a response back in the client; however, the response text from the XmlObject is completely blank!?!

如果有人能说清楚这一点,那就太棒了!

If anybody can shed any light on this, that would be great!

编辑:我认为我的AJAX调用结构正确,因为我收到Python的响应(我已经检查过调试模式);但是,当我收到一个对象时,我没有得到任何回复responseText。

I think my AJAX call is structured correctly since I am getting responses from Python (I've checked in debug mode); however, I am not getting any message back responseText when I get an object back.

推荐答案

确保你的回复 send_header(),内容类型。没有这个,我看到AJAX请求混淆了。您也可以尝试将POST切换到GET进行调试,并确保浏览器可以看到内容。

Make sure your response has a send_header() with a content type. I have seen AJAX requests get confused without this. You can also try switching your POST to a GET for debugging and make sure the browser can see the content.

如果您将查询或浏览器指向 127.0.0.1/test ,这是一个简单的HTTP示例,用于返回XML:

Here is a simple HTTP example for returning XML if you point your query or browser to 127.0.0.1/test:

import SimpleHTTPServer, SocketServer
import urlparse

PORT = 80

class MyHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
   def do_GET(self):

       # Parse query data & params to find out what was passed
       parsedParams = urlparse.urlparse(self.path)
       queryParsed = urlparse.parse_qs(parsedParams.query)

       # request is either for a file to be served up or our test
       if parsedParams.path == "/test":
          self.processMyRequest(queryParsed)
       else:
          # Default to serve up a local file 
          SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self);

   def processMyRequest(self, query):

       self.send_response(200)
       self.send_header('Content-Type', 'application/xml')
       self.end_headers()

       self.wfile.write("<?xml version='1.0'?>");
       self.wfile.write("<sample>Some XML</sample>");
       self.wfile.close();

Handler = MyHandler

httpd = SocketServer.TCPServer(("", PORT), Handler)

print "serving at port", PORT
httpd.serve_forever()

这篇关于麻烦让SimpleHTTPRequestHandler响应AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆