Nodejs how to set the pre-request header when facing non-simple cross-domain. Please advise! ! !

  node.js, question

How does nodejs handle the pre-requested header handling when non-simple cors. The following is my code, but it didn’t pass.

Node code:

var http = require('http');
 var server = http.createServer(function(req, res) {
 res.writeHeader(200,{
 'content-type':'application/json',
 'Access-Control-Allow-Origin' : '*',
 'Access-Control-Request-Method':'POST,GET,OPTIONS',
 'Access-Control-Request-Headers':'content-type'});
 res.on('data',function(d){
 console.log(d);
 })
 var a={
 'name':'test'
 };
 console.log(a);
 res.write(JSON.stringify(a));
 res.end();
 }).listen('8080');

Front end code:

<script type="text/javascript">
        var xhr=new XMLHttpRequest();
        xhr.onreadystatechange=function(){
            if(xhr.readyState==4){
                if (xhr.status>=200&&xhr.status<300) {
                    var req=xhr.responseText;
                    console.log(req);
                    console.log(xhr.getAllResponseHeaders())
                    document.getElementById('test').innerHTML=req;
                }
            }
        }
        xhr.open('post','http://127.0.0.1:8080/');
        var a={
            'name':'baihaihui'
        }
        var data=String(JSON.stringify(a));
        xhr.setRequestHeader('content-type','application/json');
        xhr.send(data);
    </script>

图片描述

var responseHeaders = {
 "access-control-allow-origin": "*",
 "access-control-allow-methods": "GET, POST, PUT, DELETE, OPTIONS",
 "access-control-allow-headers": "content-type, accept",
 "access-control-max-age": 10,
 "Content-Type": "application/json"
 };

I used google to find the correct header settings, hoping to help you. (Note that allow-headers needs to be added with’ Accept’. What’s more, my original header setting is wrong. Not access-control-request-headers. should be access-control-allow-headers.