[英]window.location.href not working in chrome and ie
我試圖使用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);
});
根據您的代碼,在執行Ajax之前將調用redirectUser(str)函數。 您可以將ajax中的redirectUser(str)保留下來。 因此,str將為-1。
$.ajax({
type: "Post",
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");
}
}).done(function(){
redirectUser(str);
});
我發現了錯誤,並使用以下代碼解決了該錯誤:
document.write("You will be redirected to main page in 5 sec.\n" + "<script>window.location.href ='http://google.com'; </script>");
//but this was not working because you have already passed data to headers, but not the redirect code.
setTimeout(redirect_approved(), 5 * 1000);
實際上創建了新頁面,因此頁面重定向不起作用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.