AngularJS&Node.js CORS不适用于PUT / POST
我的应用程序有一个CORS问题。
我的堆栈是与Express 4和AngularJS的Node.js
我已经尝试了一些东西,但我不断收到所有POST / PUT请求:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
在服务器端,我使用npm cors
模块启用了CORS
var cors = require('cors'); var corsOptions = { origin: [ 'http://www.localhost:5555', 'http://www.somedomain.com' ], methods: ['GET', 'HEAD', 'PUT', 'PATCH', 'POST', 'DELETE', 'OPTIONS'], allowedHeaders: [ 'Content-Type', 'Content-Length', 'Content-Range', 'Content-Disposition', 'Content-Description', 'Access-Control-Allow-Origin', 'Access-Control-Allow-Headers', 'Authorization', 'Origin', 'X-Requested-With', 'X-AUTH-TOKEN' ], credentials: true }; app.use(cors(corsOptions));
在AngularJS部分我使用这个:
// App Config App.config(['$httpProvider', function ($httpProvider) { $httpProvider.defaults.withCredentials = true; } ]); // Request in the Angular service var deferred = $q.defer(); var host = 'http://www.localhost:5555'; var id = 'some-id'; var data = {}; $http.put(host + '/roles/' + id, data). success(function (data) { deferred.resolve(data); }). error(function (err) { deferred.reject(err); }); return deferred.promise;
GET请求正常工作。 PUT / POST请求不断返回上面相同的错误。 预检选项是成功的,但是。
谢谢
这在Express 4服务器上适用于COR
app.use(function(req, res, next) { res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Methods", "OPTIONS, POST, GET, PUT, DELETE"); res.header("Access-Control-Allow-Headers", YOUR HEADERS HERE); res.header('Access-Control-Allow-Credentials', true); if ('OPTIONS' == req.method) { return res.sendStatus(200); } else { next(); } });
- 数字types错误与mongoose模型?
- 来自AngularJS的NodeJS HTTP请求
- 如何使用node.js中的excel4node.js以编程方式创build单元格下拉列表?
- 什么是创buildFirebasepipe理界面的正确方法? 如何在AngularJS应用程序中使用Firebase以pipe理员帐户login?
- Visual Studio 2015不识别Ionic和angularjs参数
- 使用命名约定的W / Breeze Angular mySql Node Express堆栈的exception
- 在浏览器中直接访问URL时,angular度路由不起作用,但在点击后工作?
- 如何将来自Node的传入文件stream保存到AngularJS中
- 如果只有本地的Angular App将其称为控制器,则执行路由