Ajax jQuery 调用上的 415 错误 - Spring MVC 控制器
Posted
技术标签:
【中文标题】Ajax jQuery 调用上的 415 错误 - Spring MVC 控制器【英文标题】:415 Error On Ajax jQuery Call - Spring MVC Controller 【发布时间】:2020-05-13 13:16:18 【问题描述】:我在 ajax 调用中遇到 415-Unsupported Media Type 错误。
我在 *** 上尝试了针对类似问题提出的解决方案,例如向 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...
【问题讨论】:
【参考方案1】:你可以试试这个。假设您有一个名为“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
【讨论】:
通过这种方法,您必须将完整的表单映射到具有名称属性的对象字段上。正如您在我的例子中看到的,必须接收数据的控制器正在接受一个 Complex Object (其他对象和集合在其中组成)。在这种情况下,我将无法将此类字段与表单元素绑定。以上是关于Ajax jQuery 调用上的 415 错误 - Spring MVC 控制器的主要内容,如果未能解决你的问题,请参考以下文章
IIS 上的 Angular 发布到 Asp.Net(非核心)Web Api 错误 415
AxisFault:传输错误:AXIS2 客户端 (JAVA) 中出现 415 错误