XML解析错误:找不到根元素位置

Edg*_*rna 3 php xml html5 web-applications xml-parsing

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

XML Parsing Error: no root element found Location: file:///C:/xampp/htdocs/EdgarSerna95_Lab/login.html Line Number 37, Column 3:   
Run Code Online (Sandbox Code Playgroud)

XML Parsing Error: no root element found Location: file:///C:/xampp/htdocs/EdgarSerna95_Lab/php/login.phpLine Number 37, Column 3:
Run Code Online (Sandbox Code Playgroud)

这是我的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();
?>
Run Code Online (Sandbox Code Playgroud)

我已经做了一些关于xml解析错误的研究,但我仍然无法让我的项目工作,我试过谷歌Chrome和Firefox

mik*_*ent 8

AHA!今天得到了这个原因,这会让我看起来很傻,但有一天可能会帮助别人.

在我的机器上设置了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"正确"访问它.


Raj*_*Raj 6

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

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

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