我试图使用jquery重定向页面,但它不起作用。 我试图使用所有的falvors:
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;
}
});
请帮我解决问题 如果我写了网址,然后尝试,那么它也无法正常工作。 即使它没有重定向到谷歌。
答案 0 :(得分:1)
您可以使用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);
});
答案 1 :(得分:0)
根据你的代码,在执行ajax之前调用redirectUser(str)函数。您可以将redirectUser(str)保留在ajax中。所以,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);
});
答案 2 :(得分:0)
我发现错误并使用以下代码解决了这个问题:
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);
实际上新页面已创建,因此页面重定向无法正常工作