Cross-origin HTTP request in javascript

I have a problem with an HTTP call in firefox. I know that when there is cross origin, firefox first executes OPTIONS before POST recognizes access control headers. I have no problem with this code:

Net.requestSpeech.prototype.post = function(url, data) { if(this.xhr != null) { this.xhr.open("POST", url); this.xhr.onreadystatechange = Net.requestSpeech.eventFunction; this.xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8"); this.xhr.send(data); } } 

I am testing this code with a simple html that calls this function. Everything is fine, and I have an OPTIONS and POST response, and I am processing the response. But, I am trying to integrate this code with an existing application using jquery (I don’t know if this is a problem), when the transfer (data) is done in this case, the browser (firefox) does the same, first make an OPTION request, but in this case, do not get a server response and puts this message in the console:

 [18:48:13.529] OPTIONS http://localhost:8111/ [undefined 31ms] 

Undefined ... undefined is that it does not receive the answer, but the code is the same, I do not know why in this case the option does not receive the answer, does anyone have an idea?

I am debugging the application for the server, and OPTIONS on the server is fine, but it looks like the browser is not waiting for a response.

edit more later: ok I think the problem is that I am running a simple html with the SCRIPT tag, which calls the method that executes the request, fine, but in this application that does not receive a response, I have a form that does onsubmit event, I think the send event returns very quickly, and the browser does not have time to receive the OPTIONS request.

change later later: WTF, I solve the problem so that the POST request is synchronized:

 this.xhr.open("POST", url, false); 

The response response is very fast and cannot wait for the OPTION response of the browser, any ideas for this?

+6
source share
1 answer

According to the same origin policy, you cannot send a message about cross-origin, you can get around this by including sites in the iframe (if you have access to the domain) the original site contains an iframe to an external site, the internal direction is legal.

+2
source

Source: https://habr.com/ru/post/902139/


All Articles