2016-07-22 103 views
1

我试图用角的POST和PUT方法,但我不断收到错误:响应具有无效的HTTP状态码404 AngularJS

Response for preflight has invalid HTTP status code 404

我的代码:

 $http({ 
      method: "POST", 
      url: "http://localhost:4567/api/v1/project", 
      data: project_data 
     }).then(function successCallback(response) { 
      console.log(response); 
     }), 
     function errorCallback(response) { 
      console.log('error!'); 
      console.log(response); 
     } 


     $http({ 
      method: "PUT", 
      url: "http://localhost:4567/api/v1/project/1", 
      data: data 
     }).then(function successCallback(response) { 
      console.log(response); 
     }), 
     function errorCallback(response) { 
      console.log('error!'); 
      console.log(response); 
     } 

有谁知道可能会发生什么或如何解决?我发现的其他解决方案涉及服务器端代码的更改(在这种情况下,我无法访问服务器)。提前致谢!

+0

你应该通过实际的ID,而不是':id'在URL中,你能想到的最好使用'$ resource'库 –

+0

喜 - 我尝试传递':id'参数,但我仍然得到相同的错误.. –

+0

你在运行什么样的服务器? –

回答

0

如果使用Node.js的,只是这样做:

app.options('/api/v1/project/1', function(req, res, next) { 
    res.header("Access-Control-Allow-Origin", "*"); 
    res.header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Authorization, Accept,X-Requested-With"); 
    res.header("Access-Control-Allow-Methods","PUT,POST,GET,DELETE,OPTIONS"); 
    res.header("Content-Type", "application/json;charset=utf-8"); 
    res.status(204);//important 
    res.end() 
}); 
app.put('/api/v1/project/1', function(req, res, next) { 
    ...your normal code here 
}) 
相关问题