【问题标题】:XML Parsing Error: no root element found LocationXML 解析错误:找不到根元素位置
【发布时间】:2017-08-03 00:47:12
【问题描述】:

所以我正在制作一个简单的登录/注册 Web 应用程序,但我不断收到以下错误:

XML Parsing Error: no root element found Location: file:///C:/xampp/htdocs/EdgarSerna95_Lab/login.html Line Number 37, Column 3:   

XML Parsing Error: no root element found Location: file:///C:/xampp/htdocs/EdgarSerna95_Lab/php/login.phpLine Number 37, Column 3:

这是我的 login.php

<?php
header('Content-type: application/json');

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "jammer";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    header('HTTP/1.1 500 Bad connection to Database');
    die("The server is down, we couldn't establish the DB connection");
}
else {
    $conn ->set_charset('utf8_general_ci');
    $userName = $_POST['username'];
    $userPassword = $_POST['userPassword'];

    $sql = "SELECT username, firstName, lastName FROM users WHERE username = '$userName' AND password = '$userPassword'";
    $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        while($row = $result->fetch_assoc()) {
            $response = array('firstName' => $row['firstNameName'], 'lastName' => $row['lastName']);
        }
        echo json_encode($response);
    }
    else {
        header('HTTP/1.1 406 User not found');
        die("Wrong credentials provided!");
    }
}
$conn->close();
?>

我已经对 xml 解析错误进行了一些研究,但我仍然无法使我的项目正常运行,我尝试使用 Google Chrome 和 Firefox

【问题讨论】:

  • 您必须使用 URL 和本地网络服务器打开页面/脚本。将它们作为本地文件打开不会执行 PHP,而是输出 PHP 源代码,请检查浏览器的源代码视图。

标签: php xml html web-applications xml-parsing


【解决方案1】:

我在 Spring MVC 应用程序中遇到了同样的情况,因为它被声明为 void,将其更改为返回 String 解决了问题

@PostMapping()
public void aPostMethod(@RequestBody( required = false) String body) throws IOException {
System.out.println("DoSome thing" + body); 
}

@PostMapping()
public String aPostMethod(@RequestBody( required = false) String body) throws IOException {
    System.out.println("DoSome thing" + body);
    return "justReturn something";
}

【讨论】:

    【解决方案2】:

    啊哈!今天得到这个的原因会让我看起来很傻,但可能有一天会帮助某人。

    在我的机器上设置了一个 Apache 服务器,使用 PHP 等等...我收到了这个错误...然后意识到原因:我点击了有问题的 HTML 文件(即包含 Javascript/ JQuery),所以浏览器地址栏显示“file:///D:/apps/Apache24/htdocs/experiments/forms/index.html”。

    要真正使用 Apache 服务器(假设它正在运行等),您必须在浏览器的地址栏中输入 "http://localhost/experiments/forms/index.html"

    到目前为止,我一直在使用“index.php”文件,只是更改为“index.html”文件。有点问题,因为对于前者,您必须使用 localhost “正确”访问它。

    【讨论】:

      【解决方案3】:

      确保您的 php 服务器正在运行并且 php 代码位于相应的文件夹中。如果 php 不存在,我遇到了同样的问题。我还建议将您的 html 放在同一个文件夹中,以防止在测试时出现跨域错误。

      如果这不是问题,请确保 php 中的每个 SQL 调用都是正确的,并且您使用的是当前的 php 标准... PHP 变化很快,与 html、css 和 Javascript 不同,因此可能不推荐使用某些函数。

      另外,我注意到您可能没有正确收集变量,这也可能导致此错误。如果您通过表单发送变量,它们需要采用正确的格式并根据您的偏好通过 POST 或 GET 发送。例如,如果我有一个迷宫游戏的登录页面:

      HTML

          <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
          <form class="modal-content animate" method="post">
          <div class="container">
              <label><b>Username</b></label>
              <input type="text" id="usernameinput" placeholder="Enter username" name="uname" required>
              <label><b>Password</b></label>
              <input type="password" id="passwordinput" placeholder="Enter Password" name="psw" required>
              <button onclick="document.getElementById('id01').style.display='block'">Sign Up</button>
              <button type="button" id="loginsubmit" onclick="myLogin(document.getElementById('usernameinput').value, document.getElementById('passwordinput').value)">Login</button>
          </div>
          </form>
      

      JavaScript

      function myLogin(username, password){
      var datasend=("user="+username+"&pwd="+password);
      $.ajax({
          url: 'makeUserEntry.php',
          type: 'POST',
          data: datasend,
          success: function(response, status) {
              if(response=="Username or Password did not match"){
                  alert("Username or Password did not match");
              }
              if(response=="Connection Failure"){
                  alert("Connection Failure");
              }
              else{
                  localStorage.userid = response;
                  window.location.href = "./maze.html"
              }
          },
          error: function(xhr, desc, err) {
              console.log(xhr);
              console.log("Details: " + desc + "\nError:" + err);
              var response = xhr.responseText;
              console.log(response);
              var statusMessage = xhr.status + ' ' + xhr.statusText;
              var message  = 'Query failed, php script returned this status: ';
              var message = message + statusMessage + ' response: ' + response;
              alert(message);
          }
      }); // end ajax call
      }
      

      PHP

      <?php
      $MazeUser=$_POST['user'];
      $MazePass=$_POST['pwd'];
      
      
      //Connect to DB
      $servername="127.0.0.1";
      $username="root";
      $password="password";
      $dbname="infinitymaze";
      //Create Connection
      $conn = new MySQLi($servername, $username, $password, $dbname);
      //Check connetion
      if ($conn->connect_error){
          die("Connection Failed:  " . $conn->connect_error);
          echo json_encode("Connection Failure");
      }
      $verifyUPmatchSQL=("SELECT * FROM mazeusers WHERE username LIKE '$MazeUser' and password LIKE '$MazePass'");
      $result = $conn->query($verifyUPmatchSQL);
      $num_rows = $result->num_rows;
      if($num_rows>0){
          $userIDSQL =("SELECT mazeuserid FROM mazeusers WHERE username LIKE '$MazeUser' and password LIKE '$MazePass'");
          $userID = $conn->query($userIDSQL);
          echo json_encode($userID);
      }
      else{
          echo json_encode("Username or Password did not match");
      }
      
      $conn->close();
      ?>
      

      如果您包含代码的其他部分(例如 html 和 JavaScript)会有所帮助,因为我不必像这样给出自己的示例。但是,我希望这些指针对您有所帮助!

      【讨论】:

        【解决方案4】:

        假设您正在使用 javascript,您需要在回显数据之前放置一个标头:

        header('Content-Type: application/json');
        echo json_encode($response);
        

        【讨论】:

          猜你喜欢
          • 2021-01-17
          • 2017-09-08
          • 2017-09-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多