javascript - window.location.href 在 chrome 和 ie 中不起作用

标签 javascript jquery redirect

我试图使用 jquery 重定向页面,但它不起作用。 我尝试使用所有的优点:

      window.location.href = url;
      location.href = url;
      window.location = url;
      location = url;
      window.location.assign(url);

我的代码是:

      $(document).ready(function () {
                btnsubmit = $("#btnLogin");
                btnsubmit.click(function () {
                     var uName = $("#userName").val();
                     var uPass = $("#pasWord").val();
                     var str = -1;
                     $.ajax({
                          type: "Post",
                          async: false,
                          url: "Default.aspx/userLogin",
                          data: '{"userName":"' + uName + '","userPassword":"' + uPass + '"}',
                          contentType: "application/json; charset=utf-8",
                          dataType: "json",
                          success: function (response) {
                                    str1 = response.d;
                                    str = str1;
                          },
                          failure: function (msg) {
                                    alert("Please contact your administrator");
                           }
                 });
                  redirectUser(str);
               });
               redirectUser = function (str) {
                              if (str == 0) {
                                    alert("Hello you have not yet been verified.\nPlease contact your supervisor for the same.");
    }
    else if (str == 1) {

        alert("You have been approved by your supervisor.\nBut the admin has not approved you yet.");
    }
    else if (str == 2) {
        document.write("You will be redirected to main page in 5 sec.");
        setTimeout(redirect_changePassword(), 5 * 1000);
    }
    else if (str == 3) {
        document.write("You will be redirected to main page in 5 sec.");
        setTimeout(redirect_approved(), 5 * 1000);
                }
    else if (str == 4) {
                    alert("You have been rejected by your Supervisor.\nPlease contact your supervisor for the same.");
    }
    else if (str == 5) {

        alert("You were approved by your supervisor.\nBut the admin has rejected you.\nPlease contact your supervisor for the same.")
    }
    else if (str == 6) {
        document.write("You will be redirected to main page in 5 sec.");
        setTimeout(redirect_admin(), 5 * 1000);
    }
    else if (str == 7) {
        alert("Some unkonwn error has occured.\nPlease contact your administrator.");
    }
    else if (str == 8) {
                    alert("Wrong credentials");
    }
    else if (str == 9) {
                   alert("Some unkonwn error has occured.\nPlease contact your administrator.");
    }
}

redirect_changePassword = function () {
    var nextUrl = location.protocol + '//' + location.host + '/ChangePassword.aspx';
    window.location.href = nextUrl;
}
redirect_approved = function () {
    var nextUrl = window.location.protocol + "//" + window.location.host + "/UserHome.aspx";
    //window.location.href = nextUrl;
    window.location.assign(nextUrl);
    return false;
}
redirect_admin = function () {
    var nextUrl = location.protocol + "//" + location.host + "/AdminHome.aspx";
    window.location.href = nextUrl;
}

     });

请帮我解决问题 如果我写下网址然后尝试,它也不起作用。 即使它没有重定向到谷歌。

最佳答案

您可以使用 javascript Promise 或 jQuery Deferred 对象

var promise = new Promise (function (resolve, reject) {
    $.ajax({
                      type: "Post",
                      async: false,
                      url: "Default.aspx/userLogin",
                      data: '{"userName":"' + uName + '","userPassword":"' + uPass + '"}',
                      contentType: "application/json; charset=utf-8",
                      dataType: "json",
                      success: function (response) {
                                str1 = response.d;
                                str = str1;
                      },
                      failure: function (msg) {
                                alert("Please contact your administrator");
                       }
});

promise.then(function success() {
      redirectUser(str);
});

关于javascript - window.location.href 在 chrome 和 ie 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30443639/

相关文章:

javascript - jquery在html中间

javascript - 包括 Bootstrap 弹出窗口页脚中的预定义按钮

Apache mod_rewrite 将文件类型重定向到另一个域

php - Laravel 5.3 中重定向路线

javascript - 续集数据库 :migrate hanging

javascript - 简单表单提交不调用 javascript 函数(JQuery)

javascript - 如何向左右滚轮添加滚动箭头按钮

redirect - S3 将基域重定向到 key 前缀文件夹

javascript - style = "right:0px"属性在 IE9 中不起作用?

javascript - 预加载动画隐藏了所有很酷的 slider 介绍动画