How to read JSON object to WebAPI

前端 未结 4 1126
走了就别回头了
走了就别回头了 2021-01-14 23:22

I\'ve checked a few similar questions, but none of the answers seem to fit (or dumb it down enough for me). So, I have a really simple WebAPI to check if user with an email

4条回答
  •  挽巷
    挽巷 (楼主)
    2021-01-14 23:42

    Either change it to a POST to send the object you are trying to send through the request body or change you method signature to accept the email address.

    var param = { "email": "ex.ample@email.com" };
    $.ajax({
        url: "/api/users/" + param.email,
        type: "GET",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data == true) {
                // notify user that email exists
            }
            else {
                // not taken
            }             
        }                      
    });
    
    [HttpGet, Route("api/users/{emailaddress}")]
    public bool Get(string emailaddress)
    {
        string email = emailaddress;
        UserStore userStore = new UserStore();
        ApplicationUserManager manager = new ApplicationUserManager(userStore);
        ApplicationUser user = manager.FindByEmail(email);
    
        if (user != null)
        {
            return true;
        }
    
        else
        {
            return false;
        }
    }
    
    //helper class:
    public class UserResponse
    {
        public string email { get; set; }
    }
    

提交回复
热议问题