预检的响应在AngularJS中具有无效的HTTP状态代码404

时间:2016-07-22 15:53:17

标签: angularjs http

我正在尝试使用Angular的POST和PUT方法,但我一直收到错误:

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);
        }

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

1 个答案:

答案 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
})