一尘不染

415不支持的媒体类型jQuery Ajax

spring-mvc

我正在使用Maven Rest API。通过jQuery Ajax调用发出POST请求时,出现415错误。请看一下我的代码。

function getUserDetails() {
  var name = $("#name").val();
  var mobile = $("#mobile").val();
  var location = $("#location").val();
  var email = $("#email").val();

  var userDetails = {
    "name": name,
    "mobile": mobile,
    "email": email,
    "location": location
  };

  return userDetails;
}

function createuser() {
  sendRequest("registerProcess", getUserDetails(), "post");
}

function sendRequest(url, input, method) {
  $.ajax({
    url: url,
    async: false,
    data: JSON.stringify(input),
    error: function(response) {
      //displayResponseMessage(response);
      console.log("Error");
    },
    success: function(response) {
      console.log(" Successfull");
    },
    type: method,
    headers: {
      Accept: 'application/json;charset=utf-8',
      contentType: 'application/json;charset=utf-8'
    },
    dataType: 'json'
  });
}

这是我的控制器。这UserForm是POJO类别

@RequestMapping(value = "/registerProcess", method = RequestMethod.POST,consumes= MediaType.APPLICATION_JSON_VALUE)
public String addUser(@RequestBody UserForm user) 
{  
  System.out.println("Inside Controller" );
  String email = user.getEmail();
  String mobile = user.getMobile();
  System.out.println("Details :" + email + mobile);
  return "welcome";
}

提前致谢


阅读 331

收藏
2020-06-01

共1个答案

一尘不染

提供标头'Content-Type' : 'application/json',此处输入是可以直接在ajax请求中发送的对象data :input

2020-06-01