DELETE请求被解释为GET

时间:2016-10-18 19:25:02

标签: node.js express access-control same-origin-policy http-delete

使用NodeJS创建REST服务时遇到了问题。 DELETE请求在服务器端被解释为GET。

这可能是一些安全功能,以避免数据丢失,但这一刻我弄清楚出了什么问题。

我正在使用express 4.13.4,es6-promise 4.0.5和jquery 3.1.1

客户方:

deleteBear: function (bear) {
    console.log("DEL REQ","http://domain.com/api/v1/" + bear._id);//CORRECT
    var Promise = promise.Promise;
    return new Promise(function (resolve, reject) {
        $.ajax({
            url: "http://domain.com/api/v1/" + bear._id,
            method: "DELETE",
            dataType: "jsonp",
            xhrFields: {
                withCredentials: false
            },
            headers: {
              "Access-Control-Allow-Origin: ": "*",
              "Access-Control-Allow-Methods: ": "DELETE",
              "Access-Control-Allow-Headers: ": "Authorization",
              "X-HTTP-Method-Override": "DELETE",
            },
            success: function(json){
                console.log("OK",json);//OK GETS CALLED
                resolve(json);
            },
            error: function(err){
                console.log("ERR",err);
                reject(err);
            },
        });
    });
}

服务器端:

router.route('/api/v1/:id')
/* GET request. */
.get(function(req, res) {
    console.log("GET REQUEST");//THIS GETS CALLED INSTEAD OF DELETE

    res.statusCode = 200;
    res.jsonp("OK");
})
/* DELETE request. */
.delete(function(req, res) {
    console.log("DELETE REQUEST");

    res.statusCode = 200;
    res.jsonp("OK");
});

客户端控制台中未显示任何错误:

DEL REQ http://domain.com/api/v1/58060570b45c9340e916be90
OK OK

服务器控制台显示正在调用GET而不是DELETE:

GET REQUEST
GET /api/v1/58060570b45c9340e916be90?callback=jQuery31107932283916014113_1476822027044&_=1476822027046 200 26.929 ms - 652976

0 个答案:

没有答案