simple ajax request to localhost nodejs server

前端 未结 2 521
后悔当初
后悔当初 2021-01-07 07:18

I wrote very simple server :

/* Creating server */
var server = http.createServer(function (request, response) {
  response.writeHead(200, {\"Content-Type\":         


        
2条回答
  •  感情败类
    2021-01-07 07:58

    To overcome the CORS, in your node.js file write the below, based on what you need:

    // Website you wish to allow to connect
    res.setHeader('Access-Control-Allow-Origin', '*');
    
    // Request methods you wish to allow
    res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
    
    // Request headers you wish to allow
    res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
    
    // Set to true if you need the website to include cookies in the requests sent
    // to the API (e.g. in case you use sessions)
    res.setHeader('Access-Control-Allow-Credentials', true);
    

提交回复
热议问题