小编bar*_*urn的帖子

SyntaxError:JSON.parse:JSON数据的第1行第1列出现意外字符吗?

谁能告诉我为什么从硬盘驱动器访问数据时,此代码为什么能在HTML页面中完美地工作,但是当我将其添加到express和node中时,却得到了

SyntaxError:JSON.parse:JSON数据的第1行第1列出现意外字符

具有完美格式的代码。我知道我使用格式化程序对其进行了测试,甚至手动创建了一个json对象。这是代码:

<html>
    <!doctype html>
    <html>
        <head>
            <meta charset="utf-8">
            <meta name="description" content="">
            <meta name="viewport" content="width=device-width, initial-scale=1">
            <title>Untitled</title>
        </head>
        <body>

    <div id="output"></div>

          <button id="getProperty">Get Property</button>

          <script>
            document.getElementById('getProperty').addEventListener('click', getProperty);


            function getProperty() {
                fetch('2016-regular.json')
                .then((res) => res.json())
                .then((data) => {
                    let output = '<h2>Property</h2>';
                    console.log(data);
                    data.propertystandings.propertystandingsentry.forEach(function(propertyEntry){
                        output += `
                            <ul>
                                <li>id: ${propertyEntry.property.ID}</li>
                                <li>city: ${propertyEntry.property.City}</li>
                                <li>prop name: ${propertyEntry.property.Name}</li>
                                <li>prop name: ${propertyEntry.rank}</li>
                            </ul>
                        `;
                    });
                document.getElementById('output').innerHTML = output;
                })
            }
          </script>

        </body>
    </html>
</html>
</html>
But then this code in express causes the error- …
Run Code Online (Sandbox Code Playgroud)

javascript json fetch express

1
推荐指数
1
解决办法
2574
查看次数

标签 统计

express ×1

fetch ×1

javascript ×1

json ×1