javascript - Ajax redirect on error - Stack Overflow

I'm calling php file through ajax call and if it returns nothing i want to redirect user to anothe

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";
    }
  });
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744254205a4565316.html

相关推荐

  • javascript - Ajax redirect on error - Stack Overflow

    I'm calling php file through ajax call and if it returns nothing i want to redirect user to anothe

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信