0

I have a simple jquery form which gets the input from user and posts the data to another page and displays a success message. But my problem is the data doesn't get posted to the other page, instead the error message gets popped up.

Here is my Jquery:

 $(document).ready(function () {
     $("#register").click(function () {
    var name = $("#name").val();
    var email = $("#email").val();
    var password = $("#password").val();
    var cpassword = $("#cpassword").val();
    if (name == '' || email == '' || password == '' || cpassword == '') {
        alert("Please fill all fields...!!!!!!");
    } else if ((password.length) < 8) {
        alert("Password should atleast 8 character in length...!!!!!!");
    } else if (!(password).match(cpassword)) {
        alert("Your passwords don't match. Try again?");
    } else {

            $.ajax({

            type:"POST",
            url: "SignUp.aspx/register",    //trying to post to this url
            data: { firstName: name },

            success: function (msg) {
                alert("Success");        // Success message which should pop up if successful
            },
            error: alert("Failed")       //error message which should pop up if there is an error
        });
    }
});
});

Here is the code in signup.aspx page (code behind class):

         public string register(String firstName)
         {
            return "Success";            
         }

The url i have provided is correct there is no problem with that. What might be the problem here ?

Also are there another methods to send data to another page?

Thank you for your time

Troller
  • 1,108
  • 8
  • 29
  • 47

3 Answers3

1

This

        success: function (msg) {
            alert("Success");        // Success message which should pop up if successful
        },
        error: alert("Failed")       //error message which should pop up if there is an error
    });

Should be:

        success: function (msg) {
            alert("Success");        // Success message which should pop up if successful
        },
        error: function(){alert("Failed")}       //error message which should pop up if there is an error
    });

You could have additional errors though.

sites
  • 21,417
  • 17
  • 87
  • 146
1

Your ajax call is not correctly formatted. It should be:

$.ajax({
        type:"POST",
        url: "SignUp.aspx/register",    //trying to post to this url
        data: { firstName: name })
        .done(function(data)
         {
            alert(JSON.stringify(data));
         })
        .success(function (msg) {
            alert("Success");        // Success message which should pop up if successful
        })
        .fail(function(err){
            alert("Failed");       //error message which should pop up if there is an error
        });
nixkuroi
  • 2,259
  • 1
  • 19
  • 25
0

Try setting contentType option of your ajax request,

contentType:'application/x-www-urlencoded; UTF-8',

I hope that solves your problem.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257
Purna Pilla
  • 639
  • 5
  • 3