我在端口3000(A http://localhost:3000)
上运行的node.js上有应用程序构建以下是允许跨域请求的规则:
app.use(function (req, res, next) {
// Website you wish to allow to connect
res.setHeader('Access-Control-Allow-Origin', 'http://localhost:64263');
// Request methods you wish to allow
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
// Request headers you wish to allow
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
// Set to true if you need the website to include cookies in the requests sent
// to the API (e.g. in case you use sessions)
//res.setHeader('Access-Control-Allow-Credentials', true);
// Pass to next layer of middleware
next();
});
我在apache(B)上配置了虚拟主机:
<VirtualHost *:80>
ServerName live-chat.dev
DirectoryIndex index.html
DocumentRoot C:\xampp\projects\service
Header always set Access-Control-Allow-Origin "*"
ProxyRequests off
ProxyPreserveHost On
<Proxy *>
Order deny,allow
Allow from all
</Proxy>
ProxyPass / http://localhost:3000/
ProxyPassReverse / http://localhost:3000/
</VirtualHost>
我还有另一个使用php开发的Web应用程序(C)。 问题是:当我试图直接从A向A发送ajax请求时,它没有任何错误,但是当我向B发送请求时,它返回错误:
XMLHttpRequest cannot load http://live-chat.dev/categories. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:64263' is therefore not allowed access.