简体   繁体   中英

MVC4 Ajax Post Action Parameter Null

I've a controller with post action. Using javascript ajax to post the data, the action parameter is always null. Any suggestion ?

Routing

    config.Routes.MapHttpRoute(
        null,
        routeTemplate: "api/{controller}/{action}"
    );

Ajax

public register(data: any): bool {
    var url = this.baseUrl + "Account/Register/";
    var xhr: JQueryXHR = $.ajax({
        type: "POST",
        url: url,
        dataType: "json",
        contentType: "application/json; charset=utf-8",
        data: { id: 0, "Username": "myUser", "Password" : "myPass" },
        async: false,
        cache: false,
    });
    return (xhr.status == 200);
}

Action

[HttpPost]
[AllowAnonymous]
[Mvc.ValidateAntiForgeryToken]
public void Register(UserLogin login)

UserLogin

public class UserLogin
{
    public string Username { get; set; }
    public string Password { get; set; }
}

尝试这个:

 data: JSON.stringify({ login : { id: 0, "Username": "myUser", "Password" : "myPass" }}),

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