2017-07-04 88 views
1

這是一個與此問題非常類似的問題(Tornado POST 405: Method Not Allowed),但是對於該問題的簡單答案是還是行不通。側欄中還有很多很多類似的問題----->這些問題與Tornado無關,也沒有爲我提供解決方案。「警告:tornado.access:405」錯誤從「localhost」和「file://」起始處停止POST

現在我在OSX上使用Firefox。

我的龍捲風代碼如下:

import tornado.ioloop 
import tornado.web 

class MainHandler(tornado.web.RequestHandler): 
    def post(self): 
     self.write("Hello, world") 
    get = post # <-------------- 

application = tornado.web.Application([ 
    (r"/", MainHandler), 
]) 

if __name__ == "__main__": 
    application.listen(8888) 
    tornado.ioloop.IOLoop.instance().start() 

如果我運行GET,它工作正常,但如果我使用POST,我得到的客戶端HTTP/1.1 405 Method Not Allowed錯誤和服務器端WARNING:tornado.access:405 OPTIONS錯誤。我試圖運行在既有file://index.htmllocalhost:8000/index.html設置

的JS

我的測試JS是這樣的:

//this one returns the error 
U.Ajax.post("http://localhost:8888/", "text", "data = fez hat") 
.then(function(result) { 
    console.log(result); 
}); 

//this one works 
U.Ajax.get("http://localhost:8888/", "text", "data = fez hat") 
.then(function(result) { 
    console.log(result); 
}); 

我的Ajax代碼看起來是這樣的,如果這是任何幫助:

//tested and functional ajax code, for ease of testing 
U = {}; 
U.Ajax = {}; 
U.Ajax.send = function(getOrPost, url, dataType, data) { 
    return new Promise(function(resolve, reject) { 
     var request = new XMLHttpRequest(); 

     if(getOrPost == "GET") { 
      request.responseType = dataType || "text"; 
      request.open("GET", url); 
     } 
     else {//getOrPost == "POST" 
      request.open("POST", url); 
      request.setRequestHeader('Content-type', dataType) 
     } 


     request.onload = function() { 
      if (request.status >= 200 && request.status < 400) { 
       console.log("ajax", request.status+" "+url, request); 

       resolve(request);    

      } else { 
       request.onerror(); 
      } 
     }; 

     request.onerror = function() { 
      var err = "include src '"+url+"' does not exist"; 
      console.log(err) 
      reject(err) 
     }; 

     try { 
      request.send(data); 
     } 
     catch(e) { 
      var err = "NS_ERROR_DOM_BAD_URI: Access to restricted URI '"+url+"' denied"; 
      console.log(err) 
      reject(err) 
     } 


    }); 
} 

U.Ajax.get = function(url, responseType) { 
    return U.Ajax.send("GET", url, responseType); 
} 

U.Ajax.post = function(url, contentType, data) { 
    return U.Ajax.send("POST", url, contentType, data); 
} 

編輯 ::如果我更改龍捲風代碼等於GET POST和OPTION,它的工作,但不正確

class MainHandler(tornado.web.RequestHandler): 
    def post(self): 
     print(self.request) 
    get = options = post # <-------------- 

我不再得到一個錯誤,但是當我打印self.request看來,我的頭被設置爲 「OPTIONS」 莫名其妙

HTTPServerRequest(PROTOCOL = 'HTTP',主機= '本地主機:8888', method ='OPTIONS',uri ='/',version ='HTTP/1.1',remote_ip ='127.0.0.1',headers = {'Origin':'null','Accept-Language':'en-US, en; q = 0.5','Accept-Encoding':'gzip,deflate','Access-Control-Request-Headers':'content-type','Host':'localhost:8888','Accept':' text/html,application/xhtml + xml,application/xml; q = 0.9,/; q = 0.8','User-Agent':'Mozilla/5.0(Macintosh;英特爾Mac OS X 10.11; RV:55.0)的Gecko/20100101火狐/ 55.0' , '訪問控制請求法': 'POST', '連接': '保活'})

+0

你使用的是什麼JavaScript庫? 'U.Ajax'? – falsetru

+0

不,這是非常複製從這個https://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery –

+0

已找到一個半解決方案,更新問題 –

回答

1

我不再得到錯誤,但是當我打印self.request它似乎我的標題設置爲「選項」以某種方式

這將是CORS飛行前請求。

https://developer.mozilla.org/en-US/docs/Glossary/Preflight_request

甲CORS預檢要求是檢查以查看是否CORS協議被理解爲CORS請求。

它是一個OPTIONS請求使用兩個HTTP請求報頭:Access-Control-Request-MethodAccess-Control-Request-Headers,並且原點報頭。

預檢請求在需要時由瀏覽器自動發出;在正常情況下,前端開發人員不需要自己製作這些請求。

對於跨域AJAX請求,瀏覽器首先需要檢查遠程服務器是否要使用特定方法和/或特定請求標頭接受請求。

這是通過OPTIONS請求發生的。當服務器在響應中發出客戶想要的實際請求可接受的信號時,客戶端會發出該請求。