【问题标题】:Angular $http.delete returns error 422 (Unprocessable Entity)Angular $http.delete 返回错误 422(无法处理的实体)
【发布时间】:2015-06-23 15:04:53
【问题描述】:

当我的网页尝试调用 DELETE-rest 方法时,我遇到了这个前端问题。但是,有趣的是,当我使用 SoapUI 进行 SAME 调用时,后端可以完美运行。 这是我的函数调用:

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

Web 服务就像 secure/regulations/{id},没有给出任何答案(只是删除),正如我所说,SoapUI 调用就像一个魅力,但是一个 this浏览器中的功能没有。在标题下方:

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)

根本没有标题(在 headers 选项卡中),但在 Representations 中有一个空的 Media-Type: application/json (自动创建)。

真诚感谢任何帮助!

【问题讨论】:

  • 能否也粘贴 SoapUI 跟踪以确保操作成功?
  • 对不起,我不知道你是什么意思,或者不知道该怎么做。你能告诉我怎么做吗?
  • 您使用 SoapUI 访问的确切 URL 是什么?您设置了哪些标头配置以在 SoapUI 中获得成功的结果?您可以按照此处的说明配置 HTTP 监视器:soapui.org/http-recording/recording.html
  • 我尝试设置 HTTP 监视器,但没有成功 :((它运行,但是当遇到请求时... HTTP 500)。我使用来自 SoapUI 的信息编辑帖子

标签: angularjs rest


【解决方案1】:

尝试使用这个带有类似于 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);
        });
}

【讨论】:

  • 感谢您的回答,已经很接近了,但是您给了我这个想法!您唯一错过的是'data':"" 语句(我已在您的答案中修复它)并且delete 是保留字,因此我已将其重命名。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-24
  • 2015-11-18
相关资源
最近更新 更多