Having Trouble Getting SimpleHTTPRequestHandler to respond to AJAX

前端 未结 1 722
盖世英雄少女心
盖世英雄少女心 2021-02-08 19:44

I recently tried implementing the SimpleHTTPRequestHandler to accept AJAX requests according to this.

Although everything seems to work as far as receiving the request f

1条回答
  •  礼貌的吻别
    2021-02-08 20:13

    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.

    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("");
           self.wfile.write("Some XML");
           self.wfile.close();
    
    Handler = MyHandler
    
    httpd = SocketServer.TCPServer(("", PORT), Handler)
    
    print "serving at port", PORT
    httpd.serve_forever()
    

    0 讨论(0)
提交回复
热议问题