JavaScript不会在true语句上重定向 用户名: 密码: 函数loginme(){ var username=document.getElementById(“用户名”).value; var password=document.getElementById(“密码”).value; var params=“username=“+username+”&password=“+password; var url=“login.php”; $.ajax({ 键入:“POST”, url:url, 数据类型:“html”, 数据:params, beforeSend:函数(){ document.getElementById(“status”).innerHTML='checking…'; }, 完成:函数(){ } 成功:函数(html){ document.getElementById(“status”).innerHTML=html;//代码仅在此处执行 如果(红色==“正常”){ window.location='account.php';//我想它不会在这里执行。 警报(html);//即使在这里。 } } }
大家好,我知道,这个问题被问了很多次。但是没有确切的答案。所以我还是来问这个问题。它不会将我重定向到accounts页面。和其他人一样,我也尝试过重定向,它是有效的,但不是在实际的代码中。编写ajax已经四年了,但这是我第一次遇到这个问题问题。我不知道代码是怎么回事。即使我从头开始,它也不起作用。谢谢。JavaScript不会在true语句上重定向 用户名: 密码: 函数loginme(){ var username=document.getElementById(“用户名”).value; var password=document.getElementById(“密码”).value; var params=“username=“+username+”&password=“+password; var url=“login.php”; $.ajax({ 键入:“POST”, url:url, 数据类型:“html”, 数据:params, beforeSend:函数(){ document.getElementById(“status”).innerHTML='checking…'; }, 完成:函数(){ } 成功:函数(html){ document.getElementById(“status”).innerHTML=html;//代码仅在此处执行 如果(红色==“正常”){ window.location='account.php';//我想它不会在这里执行。 警报(html);//即使在这里。 } } },javascript,php,ajax,redirect,Javascript,Php,Ajax,Redirect,大家好,我知道,这个问题被问了很多次。但是没有确切的答案。所以我还是来问这个问题。它不会将我重定向到accounts页面。和其他人一样,我也尝试过重定向,它是有效的,但不是在实际的代码中。编写ajax已经四年了,但这是我第一次遇到这个问题问题。我不知道代码是怎么回事。即使我从头开始,它也不起作用。谢谢。 <label>Username:</label> <input name="username" id="username" type="text" value="w
<label>Username:</label>
<input name="username" id="username" type="text" value="wawa">
<label>Password:</label>
<input name="password" id="password" type="password" value="123456">
<input value="Submit" name="submit" class="submit" type="submit" onclick='loginme();'>
function loginme () {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var params = "username=" + username + "&password=" + password;
var url = "login.php";
$.ajax({
type: 'POST',
url: url,
dataType: 'html',
data: params,
beforeSend: function () {
document.getElementById("status").innerHTML = 'checking...';
},
complete: function () {
}
success: function (html) {
document.getElementById("status").innerHTML = html; // the code only executes until here
if(red == "OK") {
window.location = 'account.php'; //it does not execute here i think.
alert(html); // even here.
}
}
}
函数loginme(){
var username=document.getElementById(“用户名”).value;
var password=document.getElementById(“密码”).value;
var params=“username=“+username+”&password=“+password;
var url=“login.php”;
$.ajax({
键入:“POST”,
url:url,
数据类型:“html”,
数据:params,
beforeSend:函数(){
//document.getElementById(“status”).innerHTML='checking…';
},
完成:函数(){
},
成功:功能(响应){
如果(响应==“确定”){
window.location.href='account.php';//我想它不会在这里执行。
警报(html);//即使在这里。
}
}
});
}
用户名:
密码:
在login.php内部,如果evrything运行正常,则写入echo“OK”;退出;
,然后页面将重定向到account.php
希望这会有所帮助!
<html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function loginme() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var params = "username=" + username + "&password=" + password;
var url = "login.php";
$.ajax({
type: 'POST',
url: url,
dataType: 'html',
data: params,
beforeSend: function () {
// document.getElementById("status").innerHTML = 'checking...';
},
complete: function () {
},
success: function (response) {
if(response == "OK") {
window.location.href = 'account.php'; //it does not execute here i think.
alert(html); // even here.
}
}
});
}
</script>
</head>
<body>
<label>Username:</label>
<input name="username" id="username" type="text" value="wawa">
<label>Password:</label>
<input name="password" id="password" type="password" value="123456">
<input value="Submit" name="submit" class="submit" type="submit" onclick='loginme();'>
</body>
</html>
函数loginme(){
var username=document.getElementById(“用户名”).value;
var password=document.getElementById(“密码”).value;
var params=“username=“+username+”&password=“+password;
var url=“login1.php”;
$.ajax({
键入:“POST”,
url:url,
数据类型:“html”,
数据:params,
beforeSend:函数(){
document.getElementById(“status”).innerHTML='checking…';
},
完成:函数(){
},
成功:功能(响应){
document.getElementById(“status”).innerHTML=响应;
如果(响应==“确定”){
window.location.href='account.php';//我想它不会在这里执行。
//警报(html);//即使在这里。
}
}
});
}
用户名:
密码:
仍然不能使用
标记。但是我没有使用echo=“OK”来运行我的旧代码
response,但是使用布尔值1或0
,因此我使用intiger 1而不是字符串“OK”。在哪里声明了red
?这甚至不是有效的javascript。在ajax调用中,您永远不会关闭括号。ajax调用后没有分号。此外,您也没有括号来关闭函数。我认为这是错误的现在开始工作,我将它改为整数1,而不是来自php的表示成功的字符串消息,它可以工作{,我忘了更改。^{而且它与标签不起作用。如果有人对此进行了修复,请将其发布给我们的同事。您能解释一下我缺少了什么吗!?IDK您的答案有什么不同,但是它与“退出”一起工作还是不工作在logpin.php内部。谢谢。我将在下面发布运行的代码。这取决于您是否使用字符串或数值,在您编写好代码之前没有任何区别。退出;停止并返回,为了安全起见,我使用了它。此问题已解决。PS。如果“If语句”无法识别返回的字符串,使用布尔值。
<html>
<head>
<script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script type="text/javascript">
function loginme() {
var username = document.getElementById("username").value;
var password = document.getElementById("password").value;
var params = "username=" + username + "&password=" + password;
var url = "login1.php";
$.ajax({
type: 'POST',
url: url,
dataType: 'html',
data: params,
beforeSend: function () {
document.getElementById("status").innerHTML = 'checking...';
},
complete: function () {
},
success: function (response) {
document.getElementById("status").innerHTML = response;
if(response == "OK") {
window.location.href = 'account.php'; //it does not execute here i think.
//alert(html); // even here.
}
}
});
}
</script>
</head>
<body>
<label>Username:</label>
<input name="username" id="username" type="text" value="wawa">
<label>Password:</label>
<input name="password" id="password" type="password" value="123456">
<input value="Submit" name="submit" class="submit" type="submit" onclick='loginme();'>
<div id='status'></div>
</body>
</html>