Dharmjeet Kumar
Dharmjeet Kumar

Reputation: 533

jQuery AJAX Page Redirection Error

$.ajax({
                url: "NewUserRegistrationServlet", 
                type: "post",                   
                cache: false,
                data : "username="+username+"&password="+encodeURIComponent(pswd)+"&email="+encodeURIComponent(email),
                dataType:"xml",
                timeout: 3000,
                success: function(data) {

                    var xml_node = $('ResultSet',data);
                    var status =  xml_node.find('result').text() ;
                    var result =  xml_node.find('status').text() ;

                    if( (result > 0) && (  status == 'SUCCESS') ) {
                        alert("This Statement is getting executed");
                        //window.location.replace("login.jsp"); // Not Working
                        //window.location.href = 'http://localhost:8080/MyProj/login.jsp'; // Not Working
                        window.open = ('login.jsp','_top'); // Not Working

                    }else{
                        $("#RegisErr").siblings("p").remove();
                        $("#RegisErr").after("<p>User Registration failed! Please Try Again.</p>");
                    }
                },
                error: function(xhr, status, text) {
                    $("#RegisErr").siblings("p").remove();
                    $("#RegisErr").after("<p>User Registration failed! Please Try Again.</p>");
                }
            });

What i am doing wrong

  1. OnSubmit -> Validation of form // Working Fine
  2. If Valid -> Do Ajax Request // Working Fine
  3. On Success of Ajax -> Redirect to other JSP Page // Not Woking

EDIT

Screenshot Chrome Debugger enter image description here

Solved

 windows.location = "login.jsp"

Thanks Everyone for your help.

Upvotes: 1

Views: 1816

Answers (2)

divyenduz
divyenduz

Reputation: 2027

To make your method work i.e. one of :-

1. window.location.replace("http://stackoverflow.com");
2. window.location.href = "http://stackoverflow.com";

The browser is still submitting the form after your code runs. Add return false; to the handler to prevent that.

Otherwise, use just window.location = "http://stackoverflow.com";

Refer to this post ( window.location.href not working ) for further clarification. If you still face a problem, tag me again. I will write a detailed answer for you.

This comment is the code for your solution to work - https://stackoverflow.com/a/6094213/1366216

Upvotes: 1

Amy
Amy

Reputation: 4032

Please trim all white spaces from result. you should write following line before if block.

if(Number(result)>0 && status.trim()==="success") {

//do anything you want

}

Upvotes: 0

Related Questions