How to read JSON object to WebAPI

前端 未结 4 1127
走了就别回头了
走了就别回头了 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: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.

提交回复
热议问题