我有一个存储在变量 request_str 中的字符串,我想将该数据传递给 SimpleHTTP python Web 服务器。我不确定如何将我拥有的 AJAX 实际连接到 simpleHTTP 服务器。
这是我到目前为止设置的ajax
$.ajax({
url: "SOMEPLACE",
data: {
"key": request_str.toUpperCase()
}
});
这是我正在使用的 SimpleHTTP 服务器的 python 代码。
"""
Serves files out of its current directory
Dosen't handle POST request
"""
import SocketServer
import SimpleHTTPServer
PORT = 9090
def move():
""" sample function to be called via a URL"""
return 'hi'
class CustomHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_GET(self):
#Sample values in self for URL: http://localhost:9090/jsxmlrpc-0.3/
#self.path '/jsxmlrpc-0.3/'
#self.raw_requestline 'GET /jsxmlrpc-0.3/ HTTP/1.1rn'
#self.client_address ('127.0.0.1', 3727)
if self.path=='/move':
#This URL will trigger our sample function and send what it returns back to the browser
self.send_response(200)
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write(move()) #call sample function here
return
else:
#serve files, and directory listings by following self.path from
#current working directory
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
httpd = SocketServer.ThreadingTCPServer(('localhost', PORT),CustomHandler)
print "serving at port", PORT
httpd.serve_forever()
我问如何使用 GET 设置它的原因是因为服务器的设置方式。如果我能得到明确的解释,我愿意接受建议,并将其更改为 POST。有人告诉我我应该 json 数据,但我不确定这意味着什么。
期待帮助!