gpt4 book ai didi

javascript - Ajax 响应文本不起作用

转载 作者:行者123 更新时间:2023-12-03 11:54:07 24 4
gpt4 key购买 nike

在我的ajax中,当我运行ajax.respondText时,由于某种原因,我有一个名为ajax的变量。

在 php 代码中,我要求它返回登录的用户名。它不仅回显了用户名,而且还回显了我网站的标题,正如您在此处看到的 - Andrew在第一组代码的底部。这是唯一应该回显的内容,而不是前面的 html。

<DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Better Gamerz United</title>
<link rel="icon" href="http://www.bettergamerzunited.com/favicon.ico" type="image/x-icon"/>
<link rel="shortcut icon" href="http://www.bettergamerzunited.com/favicon.ico" type="image/x-icon"/>
<link rel="stylesheet" href="css/bootstrap.css">
<link rel="stylesheet" href="css/main.css">
</head>
<body>
<header>
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<!-- Brand and toggle get grouped for better mobile display -->
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a title="" class="navbar-brand " href="index.php">
<img src="img/BGU Logo.png" />
</a>
</div>

<!-- Collect the nav links, forms, and other content for toggling -->
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav">
<li class="active"><a href="index.php">Home</a></li>
<li><a href="#">Recrutment</a></li>
<li><a href="servers.php">Servers</a></li>
<li><a href="forums.php">Forums</a></li>
<li><a href="contactus.php">Contact Us</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li><a class="navbar-link" href="login.php">Login</a></li>
<li><a class="navbar-link" class="navbar-link"class="navbar-link" href="signup.php">Register</a></li>

</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-->
</nav>
</header>

Andrew

什么可能导致我的 header 包含在我的响应中,所以我确实更深入地运行了 console.log 只是 var ajax。这为我提供了这个

XMLHttpRequest {statusText: "OK", status: 200, responseURL: "http://www.bettergamerzunited.com/login.php", response: "<DOCTYPE html>↵<html>↵<head>↵   <meta charset="UTF-8…iv><!-- /.container-->↵    </nav>↵</header>↵↵ Andrew", responseType: ""…}onabort: nullonerror: nullonload: nullonloadend: nullonloadstart: nullonprogress: nullonreadystatechange: function () {ontimeout: nullreadyState: 4response: "<DOCTYPE html>↵<html>↵<head>↵ <meta charset="UTF-8">↵   <title>Better Gamerz United</title>↵  <link rel="icon" href="http://www.bettergamerzunited.com/favicon.ico" type="image/x-icon"/>↵  <link rel="shortcut icon" href="http://www.bettergamerzunited.com/favicon.ico" type="image/x-icon"/>↵ <link rel="stylesheet" href="css/bootstrap.css">↵ <link rel="stylesheet" href="css/main.css">↵</head>↵<body>↵   <header>↵ <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">↵     <div class="container">↵        <!-- Brand and toggle get grouped for better mobile display -->↵      <div class="navbar-header">↵        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">↵            <span class="sr-only">Toggle navigation</span>↵           <span class="icon-bar"></span>↵           <span class="icon-bar"></span>↵           <span class="icon-bar"></span>↵         </button>↵        <a title="" class="navbar-brand " href="index.php">↵            <img src="img/BGU Logo.png" />↵           </a>↵     </div>↵↵        <!-- Collect the nav links, forms, and other content for toggling -->↵        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">↵       <ul class="nav navbar-nav">↵            <li class="active"><a href="index.php">Home</a></li>↵         <li><a href="#">Recrutment</a></li>↵          <li><a href="servers.php">Servers</a></li>↵           <li><a href="forums.php">Forums</a></li>↵         <li><a href="contactus.php">Contact Us</a></li>↵        </ul>↵        <ul class="nav navbar-nav navbar-right">↵           <li><a class="navbar-link" href="login.php">Login</a></li>↵<li><a class="navbar-link" class="navbar-link"class="navbar-link" href="signup.php">Register</a></li>↵↵          </ul>↵      </div><!-- /.navbar-collapse -->↵   </div><!-- /.container-->↵  </nav>↵</header>↵↵ Andrew"responseText: "<DOCTYPE html>↵<html>↵<head>↵  <meta charset="UTF-8">↵   <title>Better Gamerz United</title>↵  <link rel="icon" href="http://www.bettergamerzunited.com/favicon.ico" type="image/x-icon"/>↵  <link rel="shortcut icon" href="http://www.bettergamerzunited.com/favicon.ico" type="image/x-icon"/>↵ <link rel="stylesheet" href="css/bootstrap.css">↵ <link rel="stylesheet" href="css/main.css">↵</head>↵<body>↵   <header>↵ <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">↵     <div class="container">↵        <!-- Brand and toggle get grouped for better mobile display -->↵      <div class="navbar-header">↵        <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">↵            <span class="sr-only">Toggle navigation</span>↵           <span class="icon-bar"></span>↵           <span class="icon-bar"></span>↵           <span class="icon-bar"></span>↵         </button>↵        <a title="" class="navbar-brand " href="index.php">↵            <img src="img/BGU Logo.png" />↵           </a>↵     </div>↵↵        <!-- Collect the nav links, forms, and other content for toggling -->↵        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">↵       <ul class="nav navbar-nav">↵            <li class="active"><a href="index.php">Home</a></li>↵         <li><a href="#">Recrutment</a></li>↵          <li><a href="servers.php">Servers</a></li>↵           <li><a href="forums.php">Forums</a></li>↵         <li><a href="contactus.php">Contact Us</a></li>↵        </ul>↵        <ul class="nav navbar-nav navbar-right">↵           <li><a class="navbar-link" href="login.php">Login</a></li>↵<li><a class="navbar-link" class="navbar-link"class="navbar-link" href="signup.php">Register</a></li>↵↵          </ul>↵      </div><!-- /.navbar-collapse -->↵   </div><!-- /.container-->↵  </nav>↵</header>↵↵ Andrew"responseType: ""responseURL: "http://www.bettergamerzunited.com/login.php"responseXML: nullstatus: 200statusText: "OK"timeout: 0upload: XMLHttpRequestUploadwithCredentials: false__proto__: XMLHttpRequest

其中的响应是我的标题。

我要求的是查看下面的代码并尝试帮助我理解为什么会发生这种情况。我很困惑,我已尽我所能来解决它​​。

有问题的文件

<?php 
include 'core/init.php';
include 'includes/overall/header.php';

// If user is already logged in, header that weenis away
if($user_ok == true){
header("location: user.php?u=".$_SESSION["username"]);
exit();
}

// AJAX CALLS THIS LOGIN CODE TO EXECUTE
if(isset($_POST["e"])){
// GATHER THE POSTED DATA INTO LOCAL VARIABLES AND SANITIZE
$e = mysqli_real_escape_string($mysqli, $_POST['e']);
$p = md5($_POST['p']);
// GET USER IP ADDRESS
$ip = preg_replace('#[^0-9.]#', '', getenv('REMOTE_ADDR'));
// FORM DATA ERROR HANDLING
if($e == "" || $p == ""){
echo "login_failed";
exit();
} else {
// END FORM DATA ERROR HANDLING
$sql = "SELECT id, username, password FROM users WHERE email='$e' AND activated='1' LIMIT 1";
$query = mysqli_query($mysqli, $sql);
$row = mysqli_fetch_row($query);
$db_id = $row[0];
$db_username = $row[1];
$db_pass_str = $row[2];
if($p != $db_pass_str){
echo "login_failed";
exit();
} else {
// CREATE THEIR SESSIONS AND COOKIES
$_SESSION['userid'] = $db_id;
$_SESSION['username'] = $db_username;
$_SESSION['password'] = $db_pass_str;
setcookie("id", $db_id, strtotime( '+30 days' ), "/", "", "", TRUE);
setcookie("user", $db_username, strtotime( '+30 days' ), "/", "", "", TRUE);
setcookie("pass", $db_pass_str, strtotime( '+30 days' ), "/", "", "", TRUE);
// UPDATE THEIR "IP" AND "LASTLOGIN" FIELDS
$sql = "UPDATE users SET ip='$ip', lastlogin=now() WHERE username='$db_username' LIMIT 1";
$query = mysqli_query($mysqli, $sql);
echo $db_username;
exit();
}
}
exit();
}
?>
<script>
function emptyElement(x){
_(x).innerHTML = "";
}
function login(){
var e = _("email").value;
var p = _("password").value;
var status = _("status");
if(e == "" || p == ""){
status.innerHTML = "Fill out all of the form data";
} else {
_("loginbtn").style.display = "none";
status.innerHTML = 'please wait ...';
var ajax = ajaxObj("POST", "login.php");
ajax.onreadystatechange = function() {
if(ajaxReturn(ajax) == true) {
if(ajax.responseText == "login_failed"){
status.innerHTML = "Login unsuccessful, please try again.";
_("loginbtn").style.display = "block";
} else {
console.log(ajax.responseText); //debugging use
window.location = "user.php?u="+ajax.responseText; // This is where the header is being added. I need the word anderw here with out my header being included.
}
}
}
ajax.send("e="+e+"&p="+p);
}
}
</script>
<html>
<div class="container background">
<p class="heading-title">Log In</p>
<form id="loginform" class="form-horizontal" onsubmit="return false">
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email Address</label>
<div class="col-sm-10">
<input type="test"10 name="email" onfocus="emptyElement('status')" maxlength="88" class="form-control" id="email" value="" autocomplete="off" placeholder="Please enter your email">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" name="password" onfocus="emptyElement('status')" maxlength="16" class="form-control" id="password" value="" autocomplete="off" placeholder="Please enter your password">
<li class=""><a class="navbar-link" href="#">Forgot your password?</a></li>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 ">
<span class='pull-right text-danger' id="status"></span>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" id="loginbtn" onclick="login()" class="btn btn-primary pull-right">Log In</button><br><br>
</div>
</div>
</form>
</div>
</html>
<?php
include 'includes/overall/footer.php';
?>

最佳答案

这来自 php 脚本顶部的以下两个包含内容之一:

include 'core/init.php';
包含'includes/overall/header.php';

因此,在 ajax 调用时限制不需要的输出或文件包含。

关于javascript - Ajax 响应文本不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25678507/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com