【问题标题】:A POST request is giving me a 400 Bad Request一个 POST 请求给了我一个 400 Bad Request
【发布时间】:2015-03-27 04:02:58
【问题描述】:

我在我的项目中使用 angularjs。每当使用 AttachmentsFactory 发出 POST 请求时,我都会收到 400 错误请求。所有其他工厂的 POST 功能都正常工作。这是 控制器功能 `

$scope.updateAttachment = function(attachment){
        console.log(JSON.stringify(attachment));
        var response = AttachmentsFactory.update(attachment); //**here's the problem**
        response.$promise.then(function(data){
            attachment.edit = false;
        })
        /*$http.post("/rest/attachments", attachment).success(function(data){
            //Callback function here.
            //"data" is the response from the server.
        });*/   
    }

` 和工厂 AttachmentsFactory

services.factory('AttachmentsFactory', function ($resource) {
   return $resource('/rest/:eventId/attachments/:fileId', {}, {
       query: { method: 'GET', isArray: true },
       remove: {method: 'DELETE'},
       update: {method: 'POST', headers:{'Content-Type':'application/json'}}
   })
});

请求未到达服务器。作为参数传递的对象是有效的: {"name":"ccccc", "description":"cccc cccc cccc cccc cccc cccc cccc", "fileId":"55144430d18567d9335a94c7", "thumbNailId":null, "extenstion":"jpg", "visibleToPublic":false, "edit":true, "$$hashKey":"object:565" }

接收其余部分的服务器函数如下

@BodyParser.Of(BodyParser.Json.class)
public Result updateEventAttachment(String eventId) {
    try {
        logger.warn("HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE HERE ");
        JsonNode json = request().body().asJson();
        Attachment attachment = Json.fromJson(json, Attachment.class);
        logger.debug("Update Request received eventId ={}, fileid{}", eventId, attachment.getFileId());
        Event event = eventService.getEvent(eventId);
        Set<Attachment> attachments = event.getAttachments();
        logger.debug(" Size of attachments = {}", attachments.size());
        attachments.remove(attachment);
        attachments.add(attachment);
        eventService.updateEvent(event);
    } catch (Exception e) {
        logger.warn(" method = deleteEventAttachment exception={}",
                e.toString());
        e.printStackTrace();
    }
    return ok();
}

我在 POST 方法中添加了字段 headers:{'Content-Type':'application/json'},但没有帮助。我还尝试在 $resource 上使用 $http。还是一样。尝试用 PUT 替换 POST,还是一样。 AttachmentsFactory 的 GET 和 DELETE 函数工作正常。 谁能帮我弄清楚我做错了什么? 已经坚持了一天多。

【问题讨论】:

  • 这实质上意味着服务器期望客户端违反的结构中的数据。尝试从客户端数据中删除 $$hashkey 键,我知道这是非常特定于角度的,与您的服务器逻辑无关。
  • @TechMa9iac 是正确的。大多数情况下,我都意识到我未能创建正确的 pojo 来接收我的 Rest 层中的对象。
  • 您好,感谢您的回复。请求未到达服务器。服务器需要一个 json 对象。另外,我添加了@JsonIgnoreProperties(ignoreUnknown = true) 来接收pojo。它只会在从 JsonNode 构建对象时忽略未知字段。
  • 想知道为什么我的投票被否决
  • 当你说它没有到达服务器时,你能更具体一点吗?在 devtools 的 Network 选项卡中,是否发出了网络请求?我认为确实如此,并且您会收到 400 错误代码作为响应。那样的话,肯定是服务端配置不对,所以贴出客户端代码对我们没有用,我们需要知道服务端代码。

标签: angularjs angular-resource


【解决方案1】:

我忘记在服务器期望的前端控制器中添加一个参数。修改控制器如下解决问题。

$scope.updateAttachment = function(attachment){
    console.log(JSON.stringify(attachment));
    var response = AttachmentsFactory.update({'eventId':$routeParams.id},attachment);
    response.$promise.then(function(data){
        attachment.edit = false;
    })
}

【讨论】:

    猜你喜欢
    • 2019-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    • 2016-05-18
    相关资源
    最近更新 更多