我在 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...
}