当前位置: 首页 > 知识库问答 >
问题:

从AngularJS到Spring RestController的混合帖子提交

袁阿苏
2023-03-14

基本上,我希望能够发布一个表单与一些字段(JSON)和附件(多部分)。下面是实际工作的代码,问题是我不喜欢它,所以它主要功能是因为变通方法。

$http({
    method: 'POST',
    url: 'rest/store/logo',
    headers: {'Content-Type': undefined },
    transformRequest: function (data) {
        var formData = new FormData();
        //need to convert our json object to a string version of json otherwise the browser will do a 'toString()' on the object which will result in the value '[Object object]' on the server.
        formData.append("store", angular.toJson(data.store));
        formData.append("file", data.file);
        return formData;
    },
    data: { store: $scope.selectedStore, file: $scope.myFile } //not important but $scope.myFile comes from a directive: http://jsfiddle.net/JeJenny/ZG9re/
});
@RequestMapping(value = "/logo", method = RequestMethod.POST)
public @ResponseBody void updateLogo(HttpServletRequest request, @RequestParam(value = "store", required = false) String store, @RequestPart("file") MultipartFile file) {
    System.err.println("store: " + store); //the JSON 
    StoreEditTO storeEditTO = new Gson().fromJson(store, StoreEditTO.class);
    System.err.println("storeEditTO: " + storeEditTO);
}
    null

共有1个答案

何涵衍
2023-03-14

多亏了上面提到的评论/链接,我得到了它的工作干干净净。实际上我已经非常接近了,但是缺少{type:“application/json”}

完整解决方案:

@RequestMapping(value = "/logo", method = RequestMethod.POST, consumes = {"multipart/form-data"})
public @ResponseBody void updateLogo(@RequestPart(value = "store") StoreEditTO store, @RequestPart("file") MultipartFile file) {
}

$http({
    method: 'POST',
    url: 'rest/store/logo',
    headers: {'Content-Type': undefined },
    transformRequest: function (data) {
        var formData = new FormData();

        formData.append('store', new Blob([angular.toJson(data.store)], {
            type: "application/json"
        }));
        formData.append("file", data.file);
        return formData;
    },
    data: { store: $scope.selectedStore, file: $scope.myFile }

}).
success(function (data, status, headers, config) {
});
 类似资料:
  • 关于我昨天的文章:使用Django将请求发送到外部Rest服务-使用返回的json来更新模型 我已经设法使用django-request.post向camunda发布了数据。使用以下脚本: 我从camunda引擎得到一个错误:- 本地变量显示如下: 如何获得camunda所需的正确格式,以便在其中插入带有所需双引号的变量

  • 我正在生成自己的Wordpress模板,并希望为用户提供一个前端表单(这样他们就不必登录Wordpress仪表板)来提交一些帖子内容。多亏了本教程,我的一切都正常了,但我想做一个增强。当前,当用户按submit时,数据存储在wordpress中,然后重定向到我选择的固定URL。与此相反,我只想清除表单数据并显示一条确认消息——我想是通过AJAX?我知道wordpress中有内置的AJAX功能,但我

  • 我必须使用ajax调用向php文件提交两个值。php文件将使用$_POST[]函数处理数据。问题是我的ajax代码没有正确发送数据,所以我在console.log(result)函数中没有得到任何结果。我该如何解决这个问题?知道吗? Jquery: .PHP:

  • 我使用wordpress cms,我允许用户从前端发帖。有三种类型的用户。我自己是管理员,一个初级(作者角色)和任何来自公共部门的非登录用户。当前,如果有任何未登录的用户从前端发帖,我将被指定为具有此代码的作者。当前我的数组如下所示: 其中,我的作者id为“20”。这一切都很好。现在,我想实现的是,当我的小朋友登录并在前端创建帖子时,我希望他成为帖子作者,而不是我(目前的设置)。我知道post_作

  • 圈子帖子列表 圈子帖子详情 圈子帖子创建 圈子帖子更新 圈子帖子删除 我的帖子列表 全部帖子列表包含搜索 圈子帖子列表 GET /groups/:group/posts 响应 status 200 参数说明 名称 类型 说明 type string 默认:latest_post, latest_post 最新帖子,latest_reply最新回复 limit integer 默认 15 ,数据

  • 我有一个自定义的帖子类型和一个像这样创建的公文包分类法: 当我在查询中使用它时,它的工作原理应该是这样的。它显示在后端的菜单中,以及此自定义帖子类型的类别。 我的问题是,当我尝试从某个类别检索所有帖子时(通过单击类别名称),我发现