我在堆栈溢出中看到了很多答案,其中说设置响应 header 将使您发出“CORS”请求。但是没有解决方案对我有用。我编写了以下代码:
//Server.js Code
var express = require('express'),
app = express();
app.all('*',function(req, res, next) {
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
res.setHeader('Access-Control-Allow-Credentials', true);
res.setHeader('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE, OPTIONS');
next();
我尝试在客户端使用 $http 访问 URL 的内容:
//Controller.js
$http.get('http://domainA.com/a/ipadapi.php?id=135&client=ipad').success(function(response){
alert("I got response");
});
它在控制台中显示以下错误。
XMLHttpRequest cannot load http://domainA.com/a/ipadapi.php?id=135&client=ipad The 'Access-Control-Allow-Origin' header has a value 'http://example.xxxxx.com' that is not equal to the supplied origin. Origin 'http://localhost:3000' is therefore not allowed access.
注意:我是 NodeJS、Express 和 AngularJs 的新手
最佳答案
当您使用 CORS 传递凭据时,您需要锁定接受的来源。尝试将您的来源从 * 更改为“localhost:3000”
关于javascript - 在 Node.js/Express 和 AngularJS 中发出 CORS 请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30581954/