0

I have a jquery form which sends data to a code behind class to store that data in a local db. When the method successfully stores the data , the method returns a message which should be shown to the user through the jquery and it should then automatically re-direct the user to the login page.

I have managed to store the data to the local db which i get through the jquery form , but i have no idea of implementing the part to show the message to the user which gets returned from the method and how to redirect the user to the login page.

Here is my Jquery code

$(document).ready(function () {


$("#register").click(function () {

    var userName=$('#userName').val();
    var firstName=$("#firstName").val();
    var lastName=$("#lastName").val();
    var email = $("#email").val();
    var password= $("#password").val();
    var cpassword= $("#cpassword").val();
    var country= $("#country").val();

    var request=JSON.stringify({
        userID: userName,
        firstName: firstName,
        lastName: lastName,
        email: email,
        password: password,
        country: country,
        userType:"Normal"
    });

    if (userName == '' || 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",
            data: request,
            contentType: "application/json; charset=utf-8",
            dataType: "json",


            success: function (request) {
                alert("Successfully Registered"); //i just display this message since i don't know how to display the returned message from the method
            },
            error: function (err) { alert("Registration Failed, Please try again Later!") }
        });
    }
});
});

My code behind class is a normal c# class which returns a message saying successfully registered after storing the data. I haven't posted it here since its just a normal class.

Troller
  • 1,108
  • 8
  • 29
  • 47

2 Answers2

1

Since you're using asp.net and returning JSON from the class (which is what dataType: "json" tells me), the return object will be in the form:

{ d : /*Your JSON Object*/ }

This is because asp.net encases your object for security reasons. Returning an Array is a security risk, so asp.net ensures that you cannot return an array.

Thus, you should be able to retrieve your object by doing:

success: function (data, textStatus, jqXHR ) {
    console.log(data.d);
},
Community
  • 1
  • 1
Stryner
  • 7,288
  • 3
  • 18
  • 18
  • i am sorry i am not returning json object from the code behind class its just a string message. Please ignore the datatype :"Json". If i am just returning a string message how do i implement it. Thanks again – Troller Sep 17 '14 at 17:48
  • `dataType:'json'` tells jQuery that you're expecting JSON back from the server. If that's not the case, you should remove it from your ajax call. – Stryner Sep 17 '14 at 17:52
  • I have already removed it , sorry about that just new to ajax and thanks for all the info. But could you tell me how to simply display the returned message from the code behind class to the user. Its just a normal string value. – Troller Sep 17 '14 at 17:54
  • 1
    I haven't done much outside of JSON, but my understanding is that by default the return will be wrapped into an XML response. If you do `success: function (data, textStatus, jqXHR ) { alert(JSON.stringify(data)); },` you should see the XML string returned, from which you can parse out of the response. – Stryner Sep 17 '14 at 17:57
1
success: function (request) {
            alert(request.d)
        }