我有一個存儲在變量request_str中的字符串,我想將這些數據傳遞給SimpleHTTP python Web服務器。我不確定如何去實際連接到我的SimpleHTTP服務器的AJAX。如何使用GET設置AJAX路徑到SimpleHTTP服務器
這裏是我成立至今
$.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。有人告訴我應該對數據進行簡化處理,但我不確定這意味着什麼。
期待幫助!
我仍然遇到實現區分GET請求/解析查詢的方法時遇到問題,我認爲這是我的最後一步:( –