访问node服务器跨域,post请求变成了options,设置请求头后,获取不到post请求提交的数据var http = require('http');var url = require("url");http.createServer(function (req, res) { var urlObj = url.parse(req.url); var pathname = urlObj.pathname; // 关闭nodejs 默认访问 favicon.ico if (!pathname.indexOf('/favicon.ico')) { return; }; let requestM=req.method; res.writeHeader(200,{ "access-control-allow-origin": "*", "access-control-allow-methods": "GET, POST, PUT, DELETE, OPTIONS", "access-control-allow-headers": "Content-Type,Content-Length, Authorization, Accept,X-Requested-With", "access-control-max-age": 10, "Content-Type": "application/json" }); if(requestM==="POST"){ var post = ''; // 通过req的data事件监听函数,每当接受到请求体的数据,就累加到post变量中 req.on('data', function(chunk){ post += chunk; }); } //获取不到post 请求过来的参数}).listen(3000);
添加回答
举报
0/150
提交
取消
