Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLHttpRequest changes POST to OPTION

i have this code:

net.requestXHR = function() {     this.xhr = null;     if(window.XMLHttpRequest === undefined) {         window.XMLHttpRequest = function() {             try {                 // Use the latest version of the activex object if available                 this.xhr = new ActiveXObject("Msxml2.XMLHTTP.6.0");             }             catch(e1) {                 try {                     // Otherwise fall back on an older version                     this.xhr = new ActiveXObject("Mxsml2.XMLHTTP.3.0");                 }                 catch(e2) {                     //Otherwise, throw an error                     this.xhr = new Error("Ajax not supported in your browser");                 }             }         };     }     else         this.xhr = new XMLHttpRequest(); } net.requestXHR.prototype.post = function(url, data) {     if(this.xhr != null) {         this.xhr.open("POST", url);         this.xhr.setRequestHeader("Content-Type", "application/json");         this.xhr.send(data);     } }      var rs = new net.requestSpeech();     console.log(JSON.stringify(interaction));     rs.post("http://localhost:8111", JSON.stringify(interaction)); 

when the send execute, i have this log:

OPTIONS http://localhost:8111/ [HTTP/1.1 405 Method Not Allowed 74ms] 

And in localhost:8111 i have a reslet serverResource that accept post, it is problem of same origin policy? i have modify the restlet to put the allow-origin header and i test it with another GET http request (in jquery) and work ok. I have the problem of same origin resolve because i use an html5 browser and my server put the headers in the response, so why the send shows me this error? why change POST for OPTION? Thanks!

Possible duplicate?: I think no, but it's true, the problem is the same for both questions, but mine are refers since the question that there is an issue with the browser, and the other, first points to jquery. By experience the time does not count for duplicate, the answers are different but it's true that both questions complement each other.

like image 872
Kalamarico Avatar asked Nov 16 '11 15:11

Kalamarico


People also ask

What XMLHttpRequest method must be used to send a POST request to the server?

send() The XMLHttpRequest method send() sends the request to the server. If the request is asynchronous (which is the default), this method returns as soon as the request is sent and the result is delivered using events.

Is XMLHttpRequest deprecated?

Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience.

How can request content type be set to XML via XMLHttpRequest?

setRequestHeader('Content-Type', 'application/json') ; is added 1 line above or below the Accept header, the method used changes to OPTIONS, the Accept header changes to "text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8" and the Content-Type header disappears as if it wasn't seen.


1 Answers

Yes, this is a "problem with same-origin policy". You are making your request either to a different server or to a different port, meaning that it is a cross-site HTTP request. Here is what the documentation has to say about such requests:

Additionally, for HTTP request methods that can cause side-effects on server's data (in particular, for HTTP methods other than GET, or for POST usage with certain MIME types), the specification mandates that browsers "preflight" the request, soliciting supported methods from the server with an HTTP OPTIONS request method, and then, upon "approval" from the server, sending the actual request with the actual HTTP request method.

There is a more detailed description in the CORS standard ("Cross-Origin Request with Preflight" section). Your server needs to allow the OPTIONS request and send a response with Access-Control-Allow-Origin, Access-Control-Allow-Headers and Access-Control-Allow-Methods headers allowing the request. Then the browser will make the actual POST request.

like image 182
Wladimir Palant Avatar answered Oct 01 '22 22:10

Wladimir Palant