在对应的请求上指定 Content-Type 为 application/json。
代码示例:
$.ajax({
url: '/api/controller/action',
method: 'POST',
data: JSON.stringify({ name: 'John', age: 30 }),
contentType: 'application/json',
success: function(data) {
console.log(data);
}
});
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:12345");
var content = new StringContent(JsonConvert.SerializeObject(new { name = "John", age = 30 }));
content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
var response = await client.PostAsync("/api/controller/action", content);
if (response.IsSuccessStatusCode)
{
var data = await response.Content.ReadAsStringAsync();
Console.WriteLine(data);
}
}