2014-07-21 40 views
0

我有一個存儲在變量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。有人告訴我應該對數據進行簡化處理,但我不確定這意味着什麼。

期待幫助!

回答

1

我假設你使用JQuery是因爲這個$函數。參考JQuery文檔將有所幫助:http://api.jquery.com/jquery.ajax/

url字段是請求發送到的地方。正如任何網址,您可以通過直接輸入他們到URL GET變量:

$.ajax() { url: 'SOMEPLACE?foo=bar&hello=world }; 

但是JQuery的Ajax對象也有一個數據字段。從文檔頁面:「[數據字段]被轉換爲查詢字符串,如果還沒有字符串,則將其附加到GET請求的url」。所以另一種方式,也可能什麼本來是由json'ing提交申請將是數據,:

$.ajax() { url: 'SOMEPLACE', data: {foo: 'bar', hello: 'world'}}; 

而且缺省情況下,JQuery的Ajax請求是GET。您可以使用類型字段進行更改。

$.ajax() { url: 'SOMEPLACE', data: {var1: 'val1', var2: 'val2'}, type: 'POST'}; 

至於服務器端的Python: 我不認爲服務器找得到的變量。它只是基於url中的路徑有條件。因此,如果您通過JavaScript正確發送了GET,並且沒有收到行爲 - 這是因爲服務器端缺少邏輯。

看來,SimpleHTTPServer就是這麼簡單。所以爲了提取GET變量,你必須做一些字符串解析。考慮一下url解析函數:https://docs.python.org/2/library/urlparse.html#urlparse.parse_qs

1

對於前端AJAX調用,Toby很好地總結了它。如果你想做一個GET請求,做

$.get("http://localhost:9090/endpoint?thing1=val", .... 

然後在服務器端,你需要添加幾件事情

""" 
Serves files out of its current directory 
Dosen't handle POST request 
""" 

import SocketServer 
import SimpleHTTPServer 
from urlparse import urlparse 

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) 

    # Split get request up into components 
    req = urlparse(self.path) 

    # If requesting for /move 
    if req.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) 

    # Else if requesting /endpoint 
    elif req.path == '/endpoint': 
     # Print request query 
     print req.query 
     # Do other stuffs... 

httpd = SocketServer.ThreadingTCPServer(('localhost', PORT),CustomHandler) 

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

基本上,你只需要添加具有差異性的GET方法請求和解析他們發送的查詢數據的方法。 urlparse模塊對此很有幫助。有關如何使用它的更多文檔,請參見https://docs.python.org/2/library/urlparse.html

+0

我仍然遇到實現區分GET請求/解析查詢的方法時遇到問題,我認爲這是我的最後一步:( –