Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XML Parsing Error: no root element found Location

So I'm making a simple login/registration web application but I keep getting the following error:

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

and

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

here is my 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();
?>

I've done some research about xml parsing errors but I still cant manage to make my project work, ive tried with Google Chrome and Firefox

like image 692
Edgar Serna Avatar asked Dec 10 '22 12:12

Edgar Serna


2 Answers

AHA! Got this today for a reason which will make me look pretty silly but which might one day help someone.

Having set up an Apache server on my machine, with PHP and so on... I got this error... and then realised why: I had clicked on the HTML file in question (i.e. the one containing the Javascript/JQuery), so the address bar in the browser showed "file:///D:/apps/Apache24/htdocs/experiments/forms/index.html".

What you have to do to actually use the Apache server (assuming it's running, etc.) is go "http://localhost/experiments/forms/index.html" in the browser's address bar.

In mitigation I have up to now been using an "index.php" file and just changed to an "index.html" file. Bit of a gotcha, since with the former you are obliged to access it "properly" using localhost.

like image 174
mike rodent Avatar answered Dec 21 '22 07:12

mike rodent


I had same situation in Spring MVC Application as it was declared as void, changing it to return String solved the issue

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

To

@PostMapping()
public String aPostMethod(@RequestBody( required = false) String body) throws IOException {
    System.out.println("DoSome thing" + body);
    return "justReturn something";
}
like image 30
Raj Avatar answered Dec 21 '22 06:12

Raj