XMLHttpRequest cannot load http://localhost:8080/api/test. Origin http://localhost:3000 is not allowed by Access-Control-Allow-Origin.
我读过跨域ajax请求,了解底层的安全问题。在我的例子中,两台服务器在本地运行,并且喜欢在测试期间启用跨域请求。
localhost:8080 - Google Appengine dev server
localhost:3000 - Node.js server
当我的页面从节点服务器加载时,我发出ajax请求到localhost:8080 - GAE服务器。什么是最简单,最安全的(不想启动chrome禁用web-security选项)。如果我必须改变“内容类型”,我应该在节点服务器上这样做吗?如何?
因为它们运行在不同的端口上,所以它们是不同的JavaScript来源。它们是否在同一台机器/主机名上并不重要。
您需要在服务器(localhost:8080)上启用CORS。看看这个网站:http://enable-cors.org/
你所需要做的就是向服务器添加一个HTTP报头:
Access-Control-Allow-Origin: http://localhost:3000
或者,为了简单起见:
Access-Control-Allow-Origin: *
如果你的服务器试图设置cookie,而你使用withCredentials = true,不要使用“*”
在响应已验证的请求时,服务器必须指定一个域,并且不能使用通配符。
你可以在这里阅读更多关于withCredentials的信息
你必须让CORS来解决这个问题
如果你的应用是用简单的node.js创建的
在你的响应头中设置它
var http = require('http');
http.createServer(function (request, response) {
response.writeHead(200, {
'Content-Type': 'text/plain',
'Access-Control-Allow-Origin' : '*',
'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE'
});
response.end('Hello World\n');
}).listen(3000);
如果你的应用是用express framework创建的
使用CORS中间件
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE');
res.header('Access-Control-Allow-Headers', 'Content-Type');
next();
}
并通过
app.configure(function() {
app.use(allowCrossDomain);
//some other code
});
这里有两个参考链接
how-to-allow-cors-in-express-nodejs
dive -into-node-js-very-first-app #参见Ajax部分
我接受@火箭危险品的答案,因为它让我找到了解决方案。我需要在GAE服务器上设置标头。我必须设置这些
"Access-Control-Allow-Origin" -> "*"
"Access-Control-Allow-Headers" -> "Origin, X-Requested-With, Content-Type, Accept"
仅设置“Access-Control-Allow-Origin”出错
Request header field X-Requested-With is not allowed by Access-Control-Allow-Headers.
同样,如果需要发送认证令牌,也要添加这个
"Access-Control-Allow-Credentials" -> "true"
同样,在客户端,设置withCredentials
这将导致2个请求发送到服务器,其中一个带有OPTIONS。认证cookie没有随它一起发送,因此需要在外部认证处理。
如果有人正在寻找解决方案,如果您正在使用express,这里是快速解决方案。
const express = require('express')
const cors = require('cors')
const app = express()
1)使用NPM install cors—save
2) import it [require] const cors = require('cors')
3)使用它作为中间件app.use(cors())
详细的安装和使用cors。
就是这样,希望它能起作用。
如果您的服务器是server=express(),只需在下一行添加server.use(cors())。例如:
server.js
const express = require('express')
const cors = require('cors')
server=express()
server.use(cors())
server.get('/',(req,res)=>{
res.send({"name":"aakash","name2":"aakash4dev"})
})
server.listen(3000)
index . html
<script>
fetch('http://localhost:3000/')
.then(res=> res.json())
.then(data => console.log(data))
</script>