确保您的响应具有内容类型的send_header() 。我已经看到 AJAX 请求在没有这个的情况下会感到困惑。您还可以尝试将 POST 切换为 GET 进行调试,并确保浏览器可以看到内容。
如果您将查询或浏览器指向127.0.0.1/test ,这是一个返回 XML 的简单 HTTP 示例:
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()