简体   繁体   English

Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0 at JSON.parse (<anonymous> ) 在 XMLHttpRequest.xmlhttp.onreadystatechange</anonymous>

[英]Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) at XMLHttpRequest.xmlhttp.onreadystatechange

I want to parse data to display to my html.我想解析数据以显示到我的 html。 I get this error, but I'm not sure how to fix it.我收到此错误,但我不确定如何修复它。 Been on this for a while.有一段时间了。

I stringify the data in this file:我将此文件中的数据字符串化:

index.js: index.js:



    sqlDatabase.query("SELECT users.username, comments.comments, comments.date FROM users INNER JOIN comments ON users.user_id=comments.user_id",
        function(error, results, fields) {
            if (error) throw error;
            console.log(err);
            console.log(error);

            var object =
                JSON.parse(JSON.stringify({ results }))
            res.status(200).render('superhero-movies', object);
            // res.json(results);
            console.log(object);
        })
})

enter image description here And then on my clients where I get the error is where a parse it:在此处输入图像描述然后在我收到错误的客户端上解析它:

superhero-movies.js:超级英雄电影.js:

let username = document.querySelector('#username');
let date = document.querySelector('#date');
let comments = document.querySelector('#comments');
let submitbtn = document.querySelector('#submitbtn');
let commentOutput = document.querySelector('#message');

var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
        var object = JSON.parse(this.responseText);
        console.log(object);


        document.getElementById('output').innerHTML = '';



        var results = JSON.parse(object);

        results.forEach((user) =>

            {

                var newName = document.createElement("h5");
                var newDate = document.createElement("h5");
                let newMessage = document.createElement("h6");


                newName = document.createTextNode(object.username);
                newDate = document.createTextNode(Object.date);
                newMessage = document.createTextNode(object.comments);

                output.appendChild(newName);
                output.appendChild(newDate);
                output.appendChild(newMessage);
            });
    }
}

xmlhttp.open("GET", "/superhero-movies", true);
xmlhttp.send();

Any help is appreciated.任何帮助表示赞赏。 Can't wrap my head around this.无法解决这个问题。 Thank you in advance.先感谢您。

The problem seems to be that you make two JSON.parse问题似乎是你做了两个JSON.parse

        var object = JSON.parse(this.responseText);
        console.log(object);
        document.getElementById('output').innerHTML = '';
        // object is already an object
        var results = JSON.parse(object);

res.render expects to be passed two arguments: res.render期望通过两个 arguments:

  • A template name模板名称
  • An object which contains the key/data pairs to be inserted into that template一个object ,其中包含要插入该模板的密钥/数据对

It then, by default, outputs an HTML content-type with the data merged into the document.然后,默认情况下,它会输出 HTML 内容类型,并将数据合并到文档中。

Your second argument is a string (the output of JSON.stringify … into which the arguments you are passing are nonsense anyway).您的第二个参数是一个字符串(JSON.stringify 的JSON.stringify ......无论如何,您传递的 arguments 都是无稽之谈)。 This will get converted to an object, but not in any useful way as the only properties it has are things like the number of characters in its length .这将被转换为 object,但没有任何用处,因为它的唯一属性是其length中的字符数。

If you want to output JSON then use res.json :如果你想 output JSON 然后使用res.json

res.json(results);

 var object = JSON.parse(this.responseText); var results = JSON.parse(object);

This also makes little sense.这也没什么意义。 It is possible to double-encode JSON so you have a JSON representation of a string of JSON which in turn represents an object … but you shouldn't get yourself into that position in the first place. It is possible to double-encode JSON so you have a JSON representation of a string of JSON which in turn represents an object … but you shouldn't get yourself into that position in the first place.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未捕获的SyntaxError:JSON.parse中位置0的JSON中的意外标记a( <anonymous> ) - Uncaught SyntaxError: Unexpected token a in JSON at position 0 at JSON.parse (<anonymous>) 未捕获到的SyntaxError:JSON中的意外令牌&lt;在JSON.parse位置0处( <anonymous> ) - Uncaught SyntaxError: Unexpected token < in JSON at position 0 at JSON.parse (<anonymous>) Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token & in JSON at position 1 at JSON.parse (<anonymous>) Uncaught SyntaxError: Unexpected token &lt; in JSON at position 0 : at JSON.parse (<anonymous> ) 在对象。<anonymous> - Uncaught SyntaxError: Unexpected token < in JSON at position 0 : at JSON.parse (<anonymous>) at Object.<anonymous> Uncaught SyntaxError: JSON.parse (<anonymous> ) 在 Response.Body.json</anonymous> - Uncaught SyntaxError: Unexpected token U in JSON at position 0 at JSON.parse (<anonymous>) at Response.Body.json SyntaxError:JSON中的意外令牌u在JSON.parse( <anonymous> ) - SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>) SyntaxError: Unexpected token = in JSON at position 11 at JSON.parse (<anonymous> )</anonymous> - SyntaxError: Unexpected token = in JSON at position 11 at JSON.parse (<anonymous>) 未捕获到的SyntaxError:JSON中的意外令牌u在JSON.parse的位置0 - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse 未捕获到的SyntaxError:JSON中的意外令牌s在JSON.parse( <anonymous> ) - Uncaught SyntaxError: Unexpected token s in JSON at position 0 at JSON.parse (<anonymous>) 未捕获的语法错误:JSON 中的意外令牌 u 在 JSON.parse 的 position 0 处(<anonymous> )</anonymous> - Uncaught SyntaxError: Unexpected token u in JSON at position 0 at JSON.parse (<anonymous>)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM