无法加载预检的响应具有无效的HTTP状态代码404 axios响应请求后

时间:2017-11-29 22:27:04

标签: reactjs post http-status-code-404 axios

我有这个明确的终点:

http://localhost:3008/projects

这个终点是一个post请求函数,如下所示:

exports.create_a_project = function(req, res) {
  var new_project = new Project(req.body);
  new_project.save(function(err, project) {
    if (err)
      res.send(err);
    res.json(project);
  });
};

当我使用例如邮递员发短信时。它完美无缺:

我使用了这个对象:

{"name": "ee", "description": "eewe", "url": "ewe", "about": "ewewe","picture":"ssd"}

我的问题** 我使用axios发送此对象来自我的反应应用程序作为这样的发布请求:

const addProject = function(newProject) {

  let url = 'http://localhost:3008/projects';
  let data = {
    'name': 'ee',
    'description': 'eewe',
    'url': 'ewe',
    'about': 'ewewe',
    'picture': 'ssd',
  };
  return axios.post(url, data).then(function(response) {
    console.log(response);
  }).catch(function(error) {
    console.log(error);
  });
};
永远不会工作 我收到了这个回复:

Failed to load http://localhost:3008/projects: Response for preflight has invalid HTTP status code 404.

OPTIONS http://localhost:3008/projects 404 (Not Found)

我的后台应用程序,它的核心看起来像那样:

app.use(function(req, res, next) {
  var allowedOrigins = ['http://localhost:3000'];
  var origin = req.headers.origin;
  if(allowedOrigins.indexOf(origin) > -1){
    res.setHeader('Access-Control-Allow-Origin', origin);
  }
  res.header('Access-Control-Allow-Methods', 'GET, OPTIONS');
  res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization');
  res.header('Access-Control-Allow-Credentials', true);
  return next();
});

0 个答案:

没有答案