How to read JSON object to WebAPI

前端 未结 4 1124
走了就别回头了
走了就别回头了 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<ApplicationUser> userStore = new UserStore<ApplicationUser>();
        ApplicationUserManager<ApplicationUser> manager = new ApplicationUserManager<ApplicationUser>(userStore);
        ApplicationUser user = manager.FindByEmail(email);
    
        if (user != null)
        {
            return true;
        }
    
        else
        {
            return false;
        }
    }
    
    //helper class:
    public class UserResponse
    {
        public string email { get; set; }
    }
    
    0 讨论(0)
  • 2021-01-14 23:47

    You can create an object in your JavaScript:

    var myData= {
         Email: "me@email.com"
    };
    

    This creates an object, matching the object expected by the controller.

    You then set the Ajax call to pass this as the data property:

    $.ajax({
        url: "/api/User/",
        type: "GET",
        data: myData,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data == true) {
                // notify user that email exists
            }
            else {
                // not taken
            }             
        }                      
    });
    

    I like this approach, as you can then add more properties as required. However, if you are only passing the email address, you might want to just pass a string.

    0 讨论(0)
  • 2021-01-14 23:53
    var dto = {        
        Email: "ex.ample@email.com"
    };
    
    $.ajax({
        url: "/api/User/",
        type: "GET",
        data: dto,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (data) {
            if (data == true) {
                // notify user that email exists
            }
            else {
                // not taken
            }             
        }                      
    });
    
    0 讨论(0)
  • 2021-01-15 00:02

    Problem: Your current implementation are sending the email as an entity on a GET request. This is a problem because GET requests does not carry an entity HTTP/1.1 Methods Solution: Change the request to a POST

    Now because you are POST'ing the email from your client to your api, you have to change the API implementation to POST:

    public bool Post(UserResponse id)
    

    To make sure your posted entity is bound correctly, you can use [FromBody] like:

    public bool Post([FromBody] UserResponse id)
    

    If you do this (and you have not yet overridden the default model binder), you have to annotate your model like:

    [DataContract]
    public class UserResponse
    {
        [DataMember]
        public string email { get; set; }
    }
    

    I think that is all - hope it works :)

    0 讨论(0)
提交回复
热议问题