无法使用PUT方法(Express&Angular)

我用MEAN Stack创build了一个REST Api和一个客户端框架。 在当地工作之后,我在Heroku上部署了API。 在我的Express路线中,我处理res.headers以便CORS应该工作:

app.use('*', function(req, res, next){ res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); if(req.method === "OPTIONS") { res.header("Acces-Control-Allow-Methods", "GET, PUT, POST, DELETE"); return res.status(200).json({}); } next(); }); 

在客户端,我处理了req头文件:

 app.factory('dataServices', function($http) { return { //Get the location information from API getAll: function() { return $http({ method: 'GET', url: url }) }, get: function(id) { return $http({ method: 'GET', url: url + '/' + id }) }, post: function(id) { return $http({ method: "POST", url: url, data: {} }) }, put: function(id, data) { return $http({ method: "PUT", url: url + '/' + id, headers: { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Headers": "Origin, X-Requested-With, Content-Type, Accept", "Access-Control-Allow-Method": "PUT", "Content-Type": "application/json charset=UTF-8" }, data: data }) }, 

当我触发GET或POST请求时,没有问题。 但是当我尝试触发一个PUT或DELETE方法时:

  $scope.saveLocation = function(id) { console.log(id) dataServices.put(id).then(function successCallback(repsonse) { $scope.customer = response.data; }) }; 

我的控制台中出现以下错误:

 XMLHttpRequest cannot load https://bbsalesapi.herokuapp.com/locations/580fd2a672e68a000352783a. Method PUT is not allowed by Access-Control-Allow-Methods in preflight response. 

我试图阅读更多关于不同的http方法,但我不明白为什么它不工作。 有人能帮我这个吗?

我通常把CORS的东西放在中间件中,它适用于我…:

 // middleware var handleCORS = 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(express.bodyParser()); app.use(express.cookieParser()); app.use(express.session({ secret: 'my super secret' })); app.use(express.methodOverride()); app.use(handleCORS); app.use(app.router); app.use(express.static(__dirname + '/public')); });