0

I am posting data to an API using JQuery 3 as follows:

$.post({ url: "api/questions", data: { content: "Content" }, dataType: "json" })
  .done(function (data, status, xhr) {
    console.log(message);
  })
  .fail(function (xhr, status, error) {
    console.log(error);
  })

When I run it I get the following error:

Unsupported Media Type

I am not sure why this happens. I tested my API using PostMan to send a Post request with the following Body:

{
  content: "Content"
}

And it worked fine ... What am I missing?

Miguel Moura
  • 36,732
  • 85
  • 259
  • 481
  • Possible duplicate of [POST JSON fails with 415 Unsupported media type, Spring 3 mvc](http://stackoverflow.com/questions/11492325/post-json-fails-with-415-unsupported-media-type-spring-3-mvc) – Vlad Gincher Oct 16 '16 at 20:28

1 Answers1

2

Try using this:

$.postJSON = function(url, data, callback) {
    return jQuery.ajax({
    headers: { 
        'Accept': 'application/json',
        'Content-Type': 'application/json' 
    },
    'type': 'POST',
    'url': url,
    'data': JSON.stringify(data),
    'dataType': 'json',
    'success': callback
    });
};

(taken from this answer)

Community
  • 1
  • 1
Vlad Gincher
  • 1,052
  • 11
  • 20