1

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;
}
2
  • do you check browser console for any errors? Commented Aug 19, 2015 at 2:40
  • If my answer helped you mark it as correct ! Commented Aug 20, 2015 at 18:03

1 Answer 1

1

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;
 }
Sign up to request clarification or add additional context in comments.

2 Comments

The suggestion to change to data: dd, I don't think will help here - as the method is expecting a string, not an object (or multiple parameters). Though, you're right - it's better practice to do as you suggested, but he also needs to update the C# method to no longer take just a string parameter.
@Rob because of that I wrote how his EmailAvaiability will look like in this two cases, you ca read it !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.