Q:

Spring Rest Controller Unsupported Media Type for ajax call

I'm using simple jquery ajax on my spring rest controller and it returns an error
error: "Unsupported Media Type"
exception: "org.springframework.web.HttpMediaTypeNotSupportedException"
message: "Content type 'applicatin/json;charset=UTF-8' not supported"

but it works just fine when I use postman to send the request.
so here is my controller:
@RequestMapping(method=RequestMethod.POST, value="/bbl/submit")
    @ResponseBody
    public String saveEmail(@RequestBody BblUserSms model) {
    return "hello";
}

and here is the ajax:
$('#btnSubmit').on('click', function() {
    $.ajax({
        url: '/bbl/submit',
        method: 'POST',
        contentType: 'application/json',
        data: {
            phoneNumber: $('#phoneNumber').val();,
            email: $('#email').val(),
        },
        contentType: 'applicatin/json',
        success: function(res) {
            console.log(res);
        },
        error: function(res) {
            console.log(res);
        }
    })
});

when I send the postman request I set the content-type to application/json in header and then as for body I just use the raw type as below:
{ "phoneNumber": "123456789", "email": "something@example.com" }

and it simply works. The model in the controller gets created from the json but when I send through ajax it just doesn't reach the controller.
what could be the possible issue here?

A:

You have two contentType properties in the request, and the second one has a typo which is causing the problem. The ; after val() in the data object will also be causing problems. Try this:
$('#btnSubmit').on('click', function() {
  $.ajax({
    url: '/bbl/submit',
    method: 'POST',
    contentType: 'application/json',
    data: {
      phoneNumber: $('#phoneNumber').val(),
      email: $('#email').val(),
    },
    success: function(res) {
      console.log(res);
    },
    error: function(res) {
      console.log(res);
    }
  })
});

