简体   繁体   English

无法使PUT方法起作用(Express和Angular)

[英]Can't get the PUT method working (Express & Angular)

I've made an REST Api and a client side framework with the MEAN Stack. 我已经使用MEAN Stack制作了REST Api和客户端框架。 After working local I deployed the API on Heroku. 在本地工作后,我在Heroku上部署了API。 In my Express routes I handle the res.headers so that CORS should be working: 在我的Express路线中,我处理res.header,以便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();
});

In the client side I handled the req headers: 在客户端,我处理了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
      })
    },

When I trigger the GET or POST request there are no problems. 当我触发GET或POST请求时,没有问题。 But when I try to trigger a PUT or DELETE method: 但是,当我尝试触发PUT或DELETE方法时:

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

I get the following error in my console: 我在控制台中收到以下错误:

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

I tried to read more about the different http methods but I can't figure out why it's not working. 我试图阅读更多有关不同的http方法的信息,但我不知道为什么它不起作用。 Can somebody help me this one? 有人可以帮我吗?

I usually put CORS stuff in middleware, ad it works for me...: 我通常将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'));
});

use cors library and it will solve the issue npm install cors --save 使用cors库,它将解决问题npm install cors --save

var cors = require('cors');

 app.use(cors())

For the PUT request it's mandatory to set Content-Length header. 对于PUT请求,必须设置Content-Length标头。
Also your content Type has a problem , you forgot the ; 您的内容类型也有问题,您忘记了;

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",
      "Content-Length": Buffer.byteLength(data)
    },
    data: data
  })
},

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM