Ajax responseText不起作用

时间:2014-09-05 04:13:27

标签: javascript php ajax mysqli

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

在php代码中,我要求它返回一个登录的用户名。好吧它是回显出用户的名字,但也回显了我的网站的标题在混合中,你可以看到这里 - 安德鲁在第一组代码的底部。这是唯一应该回应的不是前面的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

什么可能导致我的标题被包含在我的响应中所以我确实更加深入并运行了一个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';
?>

1 个答案:

答案 0 :(得分:1)

这是来自php脚本顶部的以下两个中的任何一个:

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

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