简体   繁体   English

预检的响应具有无效的HTTP状态代码404 angular js

[英]Response for preflight has invalid HTTP status code 404 angular js

I am trying to prepare a Delete request in AngularJS to a nodeJS local server: 我正在尝试在AngularJS准备一个Delete请求到nodeJS本地服务器:

this.deleteMusician = function(id) {
        $http({
            url: 'http://localhost:3000/musicians/' + id,
            method: "DELETE",
            data: {}
            //processData: false,
            //headers: {'Content-Type': 'application/x-www-form-urlencoded'}
        }).success(function (data, status, headers, config) {
            console.log(data);
        }).error(function (data, status, headers, config) {
            console.log(data);
        });
    };

And my nodeJS route looks like this: 我的nodeJS路由如下所示:

app.delete('/musicians/:id', musicians.delete);

The same request via PostMan works, but on Google Chrome i get: 通过PostMan的相同请求有效,但在Google Chrome上我得到:

OPTIONS http://localhost:3000/musicians/5628eacaa972a6c5154e4162 404 (Not Found)
XMLHttpRequest cannot load http://localhost:3000/musicians/5628eacaa972a6c5154e4162. Response for preflight has invalid HTTP status code 404

CORS is enabled: CORS已启用:

var allowCrossDomain = function(req, res, next) {
  // Website you wish to allow to connect
  res.setHeader('Access-Control-Allow-Origin', 'http://localhost');

  // Request methods you wish to allow
 res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');

  // Request headers you wish to allow
  res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');

  // Set to true if you need the website to include cookies in the requests sent
  // to the API (e.g. in case you use sessions)
  res.setHeader('Access-Control-Allow-Credentials', true);

  // Pass to next layer of middleware
  next();
};

app.use(allowCrossDomain); app.use(allowCrossDomain);

You will need to configure your node server to expect options method too.Check this other answer 您需要将节点服务器配置为期望选项方法。请检查此其他答案

so: 所以:

app.options('/musicians/:id', optionsCB);

and: 和:

exports.optionsCB = function(req, res, next) {
  res.header('Access-Control-Allow-Origin', '*');
  res.header('Access-Control-Allow-Methods', 'DELETE');
  res.header('Access-Control-Allow-Headers', 'X-Requested-With,Content-Type');

  next();
}

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

相关问题 飞行前响应具有无效的HTTP状态代码404” - Response for preflight has invalid HTTP status code 404” 预检的响应具有无效的HTTP状态代码:401 angular - Response for preflight has invalid HTTP status code: 401 angular 预检的响应在Angular 2中具有无效的HTTP状态代码403 - Response for preflight has invalid HTTP status code 403 in Angular 2 AngularJS POST 失败:预检响应具有无效的 HTTP 状态代码 404 - AngularJS POST Fails: Response for preflight has invalid HTTP status code 404 XMLHttpRequest无法加载URL响应以进行预检具有无效的HTTP状态代码404 - XMLHttpRequest cannot load url Response for preflight has invalid HTTP status code 404 预检的响应具有无效的HTTP状态代码404 - ajax请求浏览器 - Response for preflight has invalid HTTP status code 404 - ajax request browser XMLHttpRequest无法加载URL。 飞行前的响应具有无效的HTTP状态代码404 - XMLHttpRequest cannot load URL. Response for preflight has invalid HTTP status code 404 预检具有无效的 HTTP 状态代码 404 Jquery AJAX POST - Preflight has invalid HTTP status code 404 Jquery AJAX POST 角度5:HTTP发布“对预检的响应具有无效的HTTP状态代码400。” - Angular 5: HTTP post `Response for preflight has invalid HTTP status code 400.` 预检的响应具有无效的HTTP状态代码400 - Response for preflight has invalid HTTP status code 400
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM