Я сталкиваюсь с ошибкой 415-Unsupported Media Type при вызове ajax.
Я попробовал предложенные решения в 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...
}