简体   繁体   中英

Jquery Ajax method not calling codebehind asp.net method

Jquery code:

<script type="text/javascript">
$(document).ready(function () {
$("#frmReg").on('submit', function (e) {
var emailAddr = $("#inputEmail").val();
            var userName = $("#userName").val();
            var password = $("#inputPassword").val();
            var FormData = {
                Email: emailAddr,
                UserName: userName,
                Password: password
            };
            var dd = JSON.stringify(FormData);
            $.ajax(
                {
                    type: "POST",
                    url: "Register.aspx/EmailAvailability",
                    contentType: "application/json; charset=utf-8",
                    data: '{"formData":'+ dd+ ' }',
                    dataType: "json",
                    success: function (data) {
                        alert("Entered");
                    },
                    fail: function () {
                        alert("failure");
                    }
                });
        });
    });
</script>

Ajax CodeBehind file: This is Asp.net method.

public static bool EmailAvailability(string formData)
{
    return true;
}

You have an error here. You already make the dd looks like Json, with JSON.stringify !

data: '{"formData":'+ dd+ ' }',

This should look like:

data: dd,

And your dd object can be done like this, if you want to be an array of items:

var uData = [];
uData[0] = emailAddr;
uData[1] = userName;
uData[2] = password;

var dd = JSON.stringify(uData: uData);

Now your web service method will look like EmailAvailability(List<string> uData)

If you want them as separate parameters:

 var dd = JSON.stringify(emailAddr: emailAddr, userName: userName, password: password);

And in this case your web service method will look like EmailAvailability(string emailAddr, string userName, string password)

Don't forget that data: dd !

PS: I also don't see the attribute [WebMethod] in the code behind in your example don't forget it !

 [WebMethod]
 public static bool EmailAvailability(string formData)
 {
     return true;
 }

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