这是一个与此问题非常类似的问题(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 –
已找到一个半解决方案,更新问题 –