当我的网页尝试调用 DELETE-rest 方法时,我遇到了这个前端问题。但是,有趣的是,当我使用 SoapUI 执行 SAME 调用时,后端工作得很好。 这是我的函数调用:
$scope.remove = function (id) {
var delUrl = "http://localhost:8080/secure/regulations/" + id;
$http.delete(delUrl);
}
Web 服务类似于 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 中,请求原始如下:
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)
根本没有 header (在 headers 选项卡中),但在 Representations 中有一个空的 Media-Type: application/json (自动创建)。
真诚地感谢任何帮助!
最佳答案
尝试使用这个带有额外 header 的详细调试版本,类似于 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);
});
}
关于angularjs - Angular $http.delete 返回错误 422(无法处理的实体),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31006502/