C#/.NET-WebApi,ContentType踩坑指南

先附上可以调通前后台的代码

前台

 1             $.ajax({
 2                 url: 'http://localhost:4405/api/applySubmit',
 3                 type: 'POST',
 4                 data: { applyData: JSON.stringify(applyData) },
 5                 ContentType: 'application/json',
 6                 success: function (returndata) {
 7                     console.log(returndata)
 8                     alert(2);
 9                 },
10                 error: function (returndata) {
11                     alert(3);
12                 }
13             });

后台

 1         public async Task<IHttpActionResult> Post(int id = 0)
 2         {
 3             Test t = new Test();
 4             t.a = "1";
 5             return Json(t);
 6         }
 7 
 8         public class Test
 9         {
10             public string a { get; set; }
11         }

 

然后关于这里踩的坑,最令人窒息的一点居然是ajax

当设置为

ContentType: 'application/json',

或者

contentType: false,

的时候,

是可以访问到后台WebApi的,

但是

contentType:"application/json",

就不可以了。

区别就在于contentType和ContentType,

是区分大小写的。

posted on 2020-12-28 17:52  奥奥没有利  阅读(1252)  评论(0)    收藏  举报