Home > database >  How can I fix this error: Uncaught SyntaxError: expected expression, got '<'?
How can I fix this error: Uncaught SyntaxError: expected expression, got '<'?

Time:02-10

I want to get the data from the JSON file, it works fine on my computer, but when I put it on my server, it shows this error.

Error: Uncaught SyntaxError: expected expression, got '<'

This is my code:

setInterval(function () {
    loadJSON(function (response) {
        jsonresponse = JSON.parse(response);
        document.getElementById('stats').innerHTML = jsonresponse[0].name;
    });
}, 1000);

function loadJSON(callback) {
    var xobj = new XMLHttpRequest();
    xobj.overrideMimeType('application/json');
    xobj.open('GET', 'data.json', true);
    xobj.onreadystatechange = function () {
        if (xobj.readyState == 4 && xobj.status == '200') {
            callback(xobj.responseText);
        }
    }
    xobj.send(null);
}
<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <title>Document</title>
        <link rel="stylesheet" href="/style.css">
    </head>
    <script src="./index.js"></script>
    <script src="./node.js"></script>
    <script src="./data.json"></script>
    </body>
</html>

Any idea of how to fix this?

CodePudding user response:

Well I fixed it by just deleting and recreating the file. Thanks for your help I appreciate it!

CodePudding user response:

Why have you added json file in script tag <script src="./data.json"></script>

Also are the files available on your server? Sometimes this error happens when the Javascript referenced file doesn't exist.

  • Related