Node.js http + cocoscreator 解决跨域访问问题

游戏内通过网址访问Node.js 的http服务器会报XMLHhttpRequest跨域访问错误,node.js端代码的res设置下header就行

const http = require('http');

http.createServer(function(req, res){

var response = 'Hello Daidai';

res.setHeader("Access-Control-Allow-Origin", "*");   //

res.setHeader("Access-Control-Allow-Headers", "X-Requested-With");

res.setHeader("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS");

res.setHeader("X-Powered-By",' 3.2.1')

res.setHeader("Content-Type", "application/json;charset=utf-8");

res.write(response);

res.end();

}).listen(8181);

你可能感兴趣的:(NodeJs)