php 返回整个 html 文件而不是字符串 (ajax)

标签 php javascript html ajax

<分区>

我在一个使用 AJAX 的网站上工作,但我遇到了死胡同。我的 javascript 有一个名为 validate 的函数,它从“名称”和“密码”字段中获取并将其发送到 php,等待响应。

function Validate() {
    var xmlhttp;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function() {

        if (xmlhttp.readyState==4 && xmlhttp.status==200) {

            if (xmlhttp.responseText.value == 'Invalid'){

                $("#box").effect('shake',150);  

                document.getElementById("validationText").innerHTML = xmlhttp.responseText;
            }
            else if (xmlhttp.reponseText.value == 'Success!'){  
            console.log(xmlhttp.responseText.value)
                document.getElementById("validationText").innerHTML = xmlhttp.responseText;


            }
        }
    }

    var name = document.getElementById("name").value;
    var password = document.getElementById("password").value;

    var arg = "JqueryPhp.php?name=" + name + "&password=" + password;

    xmlhttp.open("GET", arg, true);
    xmlhttp.send();
} 

php 文件应该返回“成功!”或“无效”,视情况而定。

<?php

    $con = mysql_connect("localhost","root","root");

    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }

      mysql_select_db("collegedatabase", $con);

      $getUserName = $_GET["name"];
      $getPassword = $_GET["password"];

      $result = mysql_query("SELECT * FROM `admin`");
      $Success = false;

      /*    if the user entered does not exist, run the function which shakes the screen and add a bit of text to say that the username is invalid  */

        while ($row = mysql_fetch_array($result)){
            if ($row['userName'] == $getUserName && $row['password'] == $getPassword){
                echo "Success!";
                $Success = true;
                break;
            }
        }
        if($Success === false) {
            echo ('Invalid');
        }


    ?>

相反,它返回一个完整的 html 文件,其中包含 body 标记中的字符串。我从 Firebug 中记录的响应如下:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>



<body>

Invalid</body>

</html>

最佳答案

在您的 PHP 代码末尾放置一个 exit;

您可能在最后运行了一些导致呈现完整 html 页面的东西。

关于php 返回整个 html 文件而不是字符串 (ajax),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11504955/

相关文章:

html - MessageChannel 和 WebSockets 之间的区别?

javascript - 如何单独显示/隐藏具有相同类名的div?

javascript - 根据服务器中的值自动完成 html block

php - stmt->get_result() 未完成

php - 在 Javascript 中解析 PHP session

页面加载后 JavaScript 函数调用不起作用

javascript - jQuery Deferred/Promises 与许多 getJSON

php - Laravel 调用未定义的方法 Config::get()

php - Javascript 验证 if 语句

javascript - 我如何优化这一长行 "if"语句?