Angular $ http.delete返回错误422(不可处理的实体)

时间:2015-06-23 15:04:54

标签: angularjs rest

当我的网页尝试调用DELETE-rest方法时,我遇到了这个前端问题。但是,有趣的部分是当我执行SAME调用但使用SoapUI时后端工作正常。 这是我的函数调用:

$scope.remove = function (id) {
     var delUrl = "http://localhost:8080/secure/regulations/" + id;
     $http.delete(delUrl);
}

网络服务就像 secure / regulations / {id} ,并且没有回答(只是删除),正如我所说的,SoapUI调用就像魅力一样,但是这个浏览器中的功能没有。下面是标题:

General
Remote Address:127.0.0.1:8080
Request URL:http://localhost:8080/secure/regulations/4
Request Method:DELETE
Status Code:422 Unprocessable Entity

Response Headers
Content-Type:application/json;charset=UTF-8
Date:Tue, 23 Jun 2015 14:28:00 GMT
Server:Apache-Coyote/1.1
Transfer-Encoding:chunked

Request Headers
Accept:application/json, text/plain, *\/*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Cookie:__ngDebug=true
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/secure/

深入研究后端的功能,当它对数据库执行 get(id)时会出现问题(在此之前, id 有值),但我不相信如果SoapUI有效,问题就在那里。 前端代码可能会遗漏一些东西:S

修改: 在SoapUI中,请求 raw 如下:

DELETE http://localhost:8080/secure/regulations/5 HTTP/1.1
Accept-Encoding: gzip,deflate
Content-Type: application/json
Content-Length: 0
Host: localhost:8080
Connection: Keep-Alive
User-Agent: Apache-HttpClient/4.1.1 (java 1.5)

根本没有标题(在标题标签中),但在 Representations 中有一个空的媒体类型:application / json (自动创建)。

任何帮助都诚实地感激不尽!

1 个答案:

答案 0 :(得分:1)

尝试使用此debug-verbose版本以及类似于SoapUI请求的额外标头:

$scope.remove = function (id) {
    var delUrl = "http://localhost:8080/secure/regulations/" + id;
    var _delete = {
         'method': 'DELETE',
         'url': delUrl,
         'headers': {
             'Content-Type': 'application/json'
         },
         'data': ""
    }

    $http(_delete)
        .success(function(){
            console.log("OK");
        })
        .error(function(data, status, headers, config){
            console.log(data);
            console.log(status);
            console.log(headers);
            console.log(config);
        });
}