Article From:https://segmentfault.com/q/1010000012136662
Question:

Ladies and gentlemen, let me ask you a few questions about low. I’ll start a service in the local area. The code is as follows:

var http = require("http");
http.createServer(function (req , res){
    res.writeHead(200,{'Content-Type':'text/plain'})
    res.write("we are is content");
    res.end();
}).listen(8899);

Then the new and HTML request code is as follows:

    var xml = new XMLHttpRequest();
    var but = document.getElementById("button");
    but.onclick=function (){
        xml.open('GET','http://localhost:8899',true);
        xml.send();
    }

The browser has been reporting the wrong, seems to cross the domain problem, the Internet also Baidu some solutions, what set up a request for the head, what set dataType=jsonp ah what have tried, although the writing of more than a year web, but HTTP for me, like at school English, there is a fear of fear.Feeling, never understand…! The most mistakes in the newspaper are:


It’s one hundred times harder to build a project than webpack.
How can we successfully deliver such a situation? To be native to Ajax;

Answer 0:
    
    var http = require("http");
http.createServer(function (req , res){
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");
    res.writeHead(200,{'Content-Type':'text/plain'})
    res.write("we are is content");
    res.end();
}).listen(8899);

Answer 1:

Let’s take a look at the two blog posts of Ruan Yi Feng, the browser’s homologous policy and its evasion method.

CORSSignal communication

I hope to help you

Answer 2:

First of all, this problem is caused by browser’s homologous strategy, so it is recommended to see what the homologous strategy is first.
Secondly, because of the restriction of homologous strategy, the problem of cross domain can be generated, and the web side has many solutions for the cross domain, such as the way you say jsonp and the cors of the upstairs reply.

Answer 3:

Cause: cross domain
Two solutions:
Front desk or backstage solution
The first is like CORS on the first floor, and the server side sets header (“Access-Control-Allow-Origin”).“);”Behind””It means that you can allow any domain access. For security reasons, you can also change the following “*” to the requested URL.
The second is the client solution, JSONP.

Answer 4:

Solving cross – domain problems in local development environment

Leave a Reply

Your email address will not be published. Required fields are marked *