gpt4 book ai didi

java - 将 JSON 数据从 JQUERY 传递到 java Controller 时出现异常

转载 作者:行者123 更新时间:2023-12-02 00:14:34 25 4
gpt4 key购买 nike

我正在将 JSON 数据从 jQuery 传递到我的 Java Controller ,并且我正在使用 @RequestBody,但我收到一个异常:

org.springframework.web.HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported

我传递的数据是:

myData = {
"source": "CSS",
"type": "CSS2",
"typeValue": "value",
"textarea_value": " desc"
}:

我用来传递此数据的 AJAX 调用是:

$.ajax({
url: './common/deleteData',
type: 'POST',
data: myData,
success: function(data) {
alert("Successfully Deleted Source..");
},
error: function(data) {}
});

我的Java Controller 如下

@RequestMapping(value = "/common/deleteData", method = RequestMethod.POST, consumes = {"application/x-www-form-urlencoded; charset=UTF-8"})
public String deleteData(@RequestBody SourceDelete sourcedelete, final HttpServletRequest request, final RedirectAttributes rdtAttribs) throws ApplicationException
{
LOGGER.entry("Deleting the Merge Preference Details");
System.out.println(sourcedelete.getSource());
return null;
}

我的 POJO 对象如下:

public class SourceDelete {
private String source;
private String type;
private String typeValue;
private String textarea_value;
//Setters and Getters
}

有人可以帮我弄清楚为什么会出现此错误以及应该如何修复它。

最佳答案

删除@RequestBody注释,

@RequestMapping(value = "/common/deleteData", method = RequestMethod.POST, consumes = {"application/x-www-form-urlencoded; charset=UTF-8"})
public String deleteData(SourceDelete sourcedelete, final HttpServletRequest request, final RedirectAttributes rdtAttribs) throws ApplicationException
{
LOGGER.entry("Deleting the Merge Preference Details");
System.out.println(sourcedelete.getSource());
return null;
}

关于java - 将 JSON 数据从 JQUERY 传递到 java Controller 时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58093668/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com