"WARNING: tornado.access: 405" POST stop error from "localhost" and "file: //" originins

This is a very similar problem to this question ( Tornado POST 405: method not resolved ), but the dead simple answer to this question still does not work. In the sidebar -----> there are also many, many similar questions that are not related to Tornado, and did not provide me with a solution.

I am currently using Firefox on OSX.

My tornado code is as follows:

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() 

If I run GET, it works fine, but if I use POST, I get an error on the client side of HTTP/1.1 405 Method Not Allowed and an error on the server side WARNING:tornado.access:405 OPTIONS . I tried to run js in both file://index.html and localhost:8000/index.html settings

My js test looks like this:

 //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); }); 

My ajax code looks like this if it helps:

 //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); } 

EDIT :: If I change the tornado code to equate GET POST and OPTION, it works, but it’s wrong

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

I no longer get the error message, but when I type self.request it seems that my headers are somehow set to "OPTIONS"

HTTPServerRequest (protocol = 'http', host = 'localhost: 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; Intel Mac OS X 10.11; rv: 55.0) Gecko / 20100101 Firefox / 55.0 ',' Access-Control-Request-Method ':' POST ',' Connection ':' keep-alive '})

+1
source share
1 answer

I no longer get the error message, but when I type self.request it seems like my headers are somehow set to "OPTIONS"

This will be a preliminary CORS request.

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

CORS Pre-Validation Request is a CORS request that verifies that the CORS protocol is understood.

This is an OPTIONS request using two HTTP request headers: Access-Control-Request-Method and Access-Control-Request-Headers and the Origin header.

A preliminary sample request is automatically issued by the browser if necessary; in normal cases, front-end developers do not need to create such queries themselves.

For cross-domain AJAX requests, the browser must first check with the remote server whether it wants to accept the request using a specific method and / or specific request headers.

This happens with an OPTIONS request. And when the server signals in the response that the actual request that the client wants to make is acceptable, the client then makes this request.

+2
source

All Articles