Potato
Potato

Reputation: 511

Server response "unsupported media type" when I send JSON format data to Asp.net Core 3.0

Visual Studio 2019 Asp.Net Core 3.0

A. Unsupported media type code

    $.ajax({
        url: this.url,
        data: jsonDataParameter,
        cache: false,
        type: "Post",
        dataType: 'JSON',
        contentType: "application/json",
        success: function (data) {}
        });

B. Successful request code

$.ajax({
    url: this.url,
    data: JSON.stringify(jsonDataParameter),
    cache: false,
    type: "Post",
    dataType: 'JSON',
    contentType: "application/json",
    success: function (data) {}
    });

Here is my questions: Is this features or Bugs? If it is features, why?

Thank you ahead.

Upvotes: 0

Views: 84

Answers (1)

Ryan
Ryan

Reputation: 20116

contentType is the type of data you're sending, and application/json; charset=utf-8 is a common one to send json data.

In your case, data {a:1,b:2} is only a Javascript object so you need to use the JSON.stringify() method to convert a JavaScript object or value to a JSON string.

Since your content type is application/json;, you need to use [FromBody] and receive the data as an object based on your situation.

Upvotes: 2

Related Questions