415不支持的媒体类型jQuery Ajax [英] 415 Unsupported Media Type jQuery Ajax

查看:223
本文介绍了415不支持的媒体类型jQuery Ajax的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using the Maven Rest API. I'm getting a 415 error when I make a POST request through a jQuery Ajax call. Please have a look on my code.

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类

This is my controller. The UserForm is a POJO Class

@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";
}

预先感谢

推荐答案

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

Provide the header as 'Content-Type' : 'application/json' and here input is an object which can be sent directly in ajax request data :input

这篇关于415不支持的媒体类型jQuery Ajax的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆