$ http DELETE变成OPTIONS AngularJS

时间:2014-01-08 15:43:07

标签: node.js angularjs rest express

当我使用put或delete时,它变成OPTIONS。我正在为我的服务器框架使用expressjs。

客户端:

$http({
    method: 'DELETE',
    url: HTTP_URL + '/update/account',
    params: { mail: mail }
});

服务器:

app.all('*', function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Credentials", true);
res.header('Access-Control-Allow-Methods', 'POST, GET, PUT, DELETE');
res.header("Access-Control-Allow-Headers",
    'Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept');
next();
});

3 个答案:

答案 0 :(得分:1)

浏览器自动发送CORS飞行前OPTIONS请求;这是正确的行为,你无法避免它。如果服务器允许原点,方法等,那么浏览器将跟进DELETE请求。

答案 1 :(得分:0)

对于CORS进行预检HTTP OPTIONS请求是否正常?您可能需要在Access-Control-Allow-Methods列表中允许OPTIONS ...

答案 2 :(得分:-1)

我建议您使用$ resource而不是$ http。它会为你节省一些样板,所以:

var appServices = angular.module('appServices', [
  'ngResource'
]);

appServices.factory('Account', ['$resource', function($resource) {
  return $resource('/accountUpdate/:accountMail', {accountMail: '@accountMail});
}]);

之后,您可以注入您的帐户服务并发出DELETE请求,如下所示:

Account.remove({accountMail: 'admin@admin.com'});

http://docs.angularjs.org/api/ngResource。$资源

相关问题