简体   繁体   中英

ASP.NET Core jQuery .ajax post always null?

ASP.NET Core 3.1 jQuery 3.3.1

I've cut down my code to the bare bones, My client side JavaScript looks like this:

    $.ajax({
        url: "/xxx/Edit",
        type: "POST",
        contentType: "application/json",
        data: JSON.stringify("Hello"),
        success: function (result) {

            //
        },
        error: function (jqXHR) {

            //
        }

My server-side Controller Action code is:

// xxx controller
public async Task<JsonResult> Edit(string data)
{
    ...
    await ...
}

The value of data is always null . It should be "Hello".

In the real code the data is not simply a string, it is a complex type, but I have found that even with a simple string (as with a complex type) the action parameter is null???

Anyone have any ideas why the controller action does not receive "Hello" in its parameter?

        $.ajax({
            url: "xxx/Edit".
            type: "POST",
            data: { data: "Hello" },  // named parameter and contentType removed
            success: function (result) {

                //
            },
            error: function (jqXHR) {

                //
            }
        });

and controller action...

    public async Task<JsonResult> Edit(string data)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM