[英]400 Bad Request on Post .Net Core API via Ajax
I am trying to call a simple method with ajax as follow:我正在尝试使用 ajax 调用一个简单的方法,如下所示:
[Route("v1/[controller]")]
[ApiController]
public class FooController : Controller
{ [HttpPost]
public FooDTO Add(Foo Foo)
{
FooDTO objFooDTO = null;
}
}
public class Foo
{
public int ID {get; set;}
public string Title {get; set;}
}
Ajax request is as follow: Ajax 请求如下:
var obj = {
"ID": 0,
"Title": "Foo Bar"
}
$.ajax({
type: "POST",
url: "http://localhost:8001/v1/Foo",
data: obj,
async: false,
dataType: "Json",
crossDomain: true,
contentType: 'application/x-www-form-urlencoded',
cache: false,
success: function (resp) {
},
error: function (e) {
}
});
Its working fine with POSTMAN
but when I try to hit via ajax error is 400
Bad request The input is not valid
.它与
POSTMAN
一起工作正常,但是当我尝试通过 ajax 命中时,错误是400
Bad request The input is not valid
。 Is there anything which I am missing ?有什么我想念的吗?
jquery
version is 1.12.0
. jquery
版本是1.12.0
。
Looks like the contentType
property supposed to be application/json
as you are passing on an object but not form data.看起来
contentType
属性应该是application/json
因为您正在传递一个对象而不是表单数据。
Also, you should serialize data object with JSON.Stringify
method so that the javascript object will be converted to json.此外,您应该使用
JSON.Stringify
方法序列化数据对象,以便将 javascript 对象转换为 json。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.