node 跨域问题
发布于 4 个月前 作者 Axiaomo 862 次浏览 来自 问答

自己写的node服务器,这个跨域问题怎么解决

var http = require(“http”); var url = require(“url”); var fs = require(“fs”);

http .createServer(function(request, response) { var params = url.parse(request.url, true).query; var dataFileName = params[“dvfile”]; var callBack = params[“callback”];

var dataset = "[50, 60, 70]";

if (dataFileName !== undefined) {
  dataset = fs.readFileSync(dataFileName, "utf8");
}

if (callBack !== undefined) {
  dataset = callBack + "(" + dataset + ")";
}

response.writeHead(200, {
  "Content-Type": "application/json; charset=utf-8"
});
response.write(dataset);
response.end();

}) .listen(1033);

5 回复

找找cors这个模块

nginx反向代理,安全

设置一下响应头

善用搜索,上面三楼的方法都阔以,不过手动设置 cors 响应头有些小坑,然而 stackoverflow 都有答案=。=

res.header(“Access-Control-Allow-Origin”, “*”); res.header(“Access-Control-Allow-Headers”, “Origin, X-Requested-With, Content-Type, Accept”);

回到顶部