這是一個與此問題非常類似的問題(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.html
和localhost:8000/index.html
設置
我的測試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', '連接': '保活'})
你使用的是什麼JavaScript庫? 'U.Ajax'? – falsetru
不,這是非常複製從這個https://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery –
已找到一個半解決方案,更新問題 –