0

我在 ajax 调用中遇到 415-Unsupported Media Type 错误。

我在 Stackoverflow 上尝试了针对类似问题提出的解决方案,例如向 ajax 调用添加标头或将 dataType 作为 json 传递,但仍然是相同的响应。

以下是我的代码,如果我遗漏了什么,请指导我:

AJAX jQuery:

var contentTypeJSON = "application/json; charset=utf-8";

var selectedFolderShareOrgs = [];
 $.each($("input[name='shareFolderOrg']:checked"), function(){
     var sharedOrgObj = new Object();
     sharedOrgObj.org = new Object();
     sharedOrgObj.org.id = $(this).val()
     selectedFolderShareOrgs.push(sharedOrgObj);
 });

var folderToCreate = new Object();
folderToCreate.shared = new Object();
folderToCreate.shared.name = folderName;
folderToCreate.parentId = parentFolderId;
folderToCreate.validUntil = null== folderValidUntilVal ? null : new Date(folderValidUntilVal);
folderToCreate.accessType = accessTypeVal;
folderToCreate.sharedGroupId = groupType;
folderToCreate.entityId = currentEntityId;
folderToCreate.entityType = currentEntityType;
folderToCreate.orgSharedAccess = selectedFolderShareOrgs;

// Sample JSON: "{"shared":{"name":"test folder"},"parentId":"-1","validUntil":"2020-01-31T19:59:59.000Z","accessType":"2","sharedGroupId":2,"entityId":"1","entityType":"Organization","orgSharedAccess":[]}"

$.ajax({
    type : "POST",
    url : contextPath+ "/secure/sharedFiles/createSharedFolder",
    data : JSON.stringify(folderToCreate),
    contentType : contentTypeJSON,
    success : function(data) {
        alert("uploaded!");
    },
    error : function(response, textStatus) {
        alert("error!");
    }
});

控制器:

@RequestMapping(value="/createSharedFolder")
public @ResponseBody boolean createSharedFolder(HttpServletRequest request,Principal principal,
        HttpSession session,Locale locale,ModelAndView mav, @RequestBody SharedFile folderToCreate){
    return createFolderUtil(request, principal, session, locale,mav, folderToCreate);
}

Java 对象:

public class SharedFile implements java.io.Serializable {

    private Shared shared;
    private Documents document;
    private Chairman chairman;
    private Organization org;       
    private Sector sector;          
    private Division division;
    private Section section;
    private BigDecimal parentId;
    private Date validUntil;
    private BigDecimal accessType;      
    private BigDecimal sharedGroupId;   
    private BigDecimal entityId;    
    private String entityType;
    private BigDecimal active;
    private Set<OrganizationSharedAccess> orgSharedAccess;
    private BigDecimal status;

    //getters and setters...
}
4

1 回答 1

0

你可以试试这个。假设您有一个名为“uploadForm”的表单 ID 和一个名为“btn-save”的提交按钮 ID。

     $("#btn-save").click(function(event) {
         event.preventDefault();
         var folderToCreate= new FormData($('#uploadForm')[0]);        
         $.ajax({type : "POST",
                 url : contextPath+ "/secure/sharedFiles/createSharedFolder",
                 data : folderToCreate,
                 contentType : false,
                 processData : false,
                 success : function(data) {
                      alert("uploaded!");
                 },
                 error : function(response, textStatus) {
                   alert("error!");
             }
      });

在控制器中:删除@RequestBody只是使用SharedFile folderToCreate

于 2020-02-19T07:19:58.183 回答