Reputation: 71
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;
}
Upvotes: 1
Views: 963
Reputation: 18127
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
!
P.S: 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;
}
Upvotes: 1