admin管理员组

文章数量:1400128

I'm calling php file through ajax call and if it returns nothing i want to redirect user to another page (It's for error reports, if it doesn't return anything it means that user logged in). Tried to add error section but it doesn't work. Any suggestions will help. Thanks! Btw, I have small jQuery function at the top of the ajax function, why it breaks my whole ajax call?

ajax.js

function loginAjax() {

//$("#email_errors").empty(); //This function doesnt work and kills whole ajax call. Here is loginAjax function call line - <button type = "submit" id = "push_button" onclick = "loginAjax(); return false">PushMe</button>

$.ajax({
url: "Classes/call_methods_login.php",
type: "POST",
dataType: "json",
data: {
login_email: $("#login_email").val(),
login_password: $("#login_password").val(),
},
success: function(data) {
$("#login_error").html(data.login_message);
}

});
}

I'm calling php file through ajax call and if it returns nothing i want to redirect user to another page (It's for error reports, if it doesn't return anything it means that user logged in). Tried to add error section but it doesn't work. Any suggestions will help. Thanks! Btw, I have small jQuery function at the top of the ajax function, why it breaks my whole ajax call?

ajax.js

function loginAjax() {

//$("#email_errors").empty(); //This function doesnt work and kills whole ajax call. Here is loginAjax function call line - <button type = "submit" id = "push_button" onclick = "loginAjax(); return false">PushMe</button>

$.ajax({
url: "Classes/call_methods_login.php",
type: "POST",
dataType: "json",
data: {
login_email: $("#login_email").val(),
login_password: $("#login_password").val(),
},
success: function(data) {
$("#login_error").html(data.login_message);
}

});
}
Share Improve this question asked Dec 10, 2014 at 0:16 Evaldas ButkusEvaldas Butkus 6654 gold badges12 silver badges32 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 3
$.ajax({
url: "Classes/call_methods_login.php",
type: "POST",
dataType: "json",
data: {
login_email: $("#login_email").val(),
login_password: $("#login_password").val(),
},
success: function(data) {
 $("#login_error").html(data.login_message);
},
error: function(){
 window.location.replace("http://stackoverflow.");
}

});
}

To redirect using javascript all you need to do is override the location.href attribute.

function loginAjax() {

  $.ajax({
    url: "Classes/call_methods_login.php",
    type: "POST",
    dataType: "json",
    data: {
      login_email: $("#login_email").val(),
      login_password: $("#login_password").val(),
    },
    // the success method is deprecated in favor of done.
    done: function(data) {
      $("#login_error").html(data.login_message);
    },
    fail: function(data) {
      location.href="path/to/error/page";
    }
  });
}

本文标签: javascriptAjax redirect on errorStack Overflow