訪問(wèn)node服務(wù)器跨域,post請(qǐng)求變成了options,設(shè)置請(qǐng)求頭后,獲取不到post請(qǐng)求提交的數(shù)據(jù)var http = require('http');var url = require("url");http.createServer(function (req, res) { var urlObj = url.parse(req.url); var pathname = urlObj.pathname; // 關(guān)閉nodejs 默認(rèn)訪問(wèn) 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 = ''; // 通過(guò)req的data事件監(jiān)聽(tīng)函數(shù),每當(dāng)接受到請(qǐng)求體的數(shù)據(jù),就累加到post變量中 req.on('data', function(chunk){ post += chunk; }); } //獲取不到post 請(qǐng)求過(guò)來(lái)的參數(shù)}).listen(3000);
node 跨域訪問(wèn)
慕容708150
2019-03-18 21:18:43