【发布时间】:2018-12-13 12:43:21
【问题描述】:
我知道关于 SO 的问题不乏CORS,但在这种情况下没有一个可以帮助我:
我想使用这个 jQuery sn-p 从本地主机后端获取 JSON 数据:
var SERVER_URL = "http://127.0.0.1:8080";
$.getJSON(SERVER_URL,function(result){
console.log("result is", result);
$.each(result, function(i, field){
$("div").append(field + " ");
});
});
sn-p 被加载到index.html,它使用这个简单的节点静态文件服务器在http://localhost:3000/index.html 上运行:
var connect = require('connect');
var serveStatic = require('serve-static');
connect().use(serveStatic(__dirname)).listen(3000, function(){
console.log('Server running on 3000...');
});
但在 Chrome 控制台中我得到:
Failed to load http://127.0.0.1:8080/: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3000' is therefore not allowed access.
我在 Firefox 中遇到了同样的问题。
我该如何解决这个问题?
【问题讨论】:
-
见github.com/cloudhead/node-static/issues/153如何传递access-control-allow-oringin header
-
@Lars-OlofKreim 你能详细说明你的答案吗?我在尝试使用该建议时遇到错误。
-
你得到的错误是什么? :)
-
@Lars-OlofKreim
ReferenceError: nodeStatic is not defined
标签: javascript jquery node.js cors