我想使用Ajax发布一些数据,但使用内容类型的应用程序/ json时(HTTP/1.1 406不可接受)但是,如果我将内容类型更改为'application/x-www-form-urlencoded',那么它确实有效。如何将JSON发送到MVC API(使用Ajax Post)
任何想法?
Ajax代码摘录:
$.ajax({
type: "POST",
data: {"hello":"test"},
url: "http://workingUrl/controller",
contentType : 'application/json',
cache: false,
dataType: "json",
.....
的Web API 2:
public IHttpActionResult Post(testModel hello)
{
/// do something here
}
型号:
public class testModel
{
public string hello {get;set;}
public testModel()
{ }
}
提琴手:
HTTP/1.1 406 Not Acceptable (In the IDE, I have a breakpoint in the Post method which is not hit).
我曾尝试添加格式化到WebAPi.config,但没有运气
config.Formatters.Add(new JsonMediaTypeFormatter());