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

标签 java jquery ajax spring

我正在将 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/

相关文章:

java - 将小数位设置为浮点值

java - 玩!框架-向后兼容性

javascript - $.ajax 超时示例错误且危险?

javascript 无法从网络驱动器运行

ajax tabcontainer 中的 Jquery datetimepicker()

java 输出单行而不是逐行

javascript - 从数据表选定行中获取最低和最高日期值

javascript - 每次选择新选项时如何触发 jQuery 函数?

c# - 未知的网络方法,尝试使用 AJAX 发送 json

Java程序执行一个命令需要很长时间