简体   繁体   English

通过 Ajax 对 Post .Net Core API 的 400 错误请求

[英]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.

相关问题 在.NET MVC API控制器中对400 post请求的错误请求 - 400 bad request for http post request in .NET MVC API controller OneFS API POST 请求上的 400 错误请求 - 400 Bad Request on OneFS API POST request 发布到Linkedin Groups API 400错误请求 - Post to Linkedin Groups API 400 Bad Request Azure ASP.NET 核心 POST 400 错误请求 Blazor Webassembly - Azure ASP.NET Core POST 400 bad request Blazor Webassembly 部署到测试服务器时,POST请求上的ASP.NET Web API“ 400错误请求” - ASP.NET Web API “400 Bad Request” on POST Request When Deploying to Test Server 从远程计算机调用Web api core asp.net总是收到400错误请求 - Calling web api core asp.net from remote computer always gets 400 bad request 如何在生产环境中禁用 Asp.Net Core web API 中的 400 Bad request 的消息正文? - how to disable 400 Bad request's message body in Asp.Net Core web API in production environment? TypeScript POST API 请求错误 400 - 错误请求 - TypeScript POST API request error 400 - Bad Request ASP.NET 核心 MVC - 为什么 Ajax JSON 请求在 POST 上返回 400 而不是 403 - ASP.NET Core MVC - Why does Ajax JSON request return 400 on POST instead of 403 通过JQuery使用WCF POST进行400错误请求HTTP响应 - 400 Bad Request HTTP Response using a WCF POST via JQuery
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM