• <small id='UDN15'></small><noframes id='UDN15'>

      <i id='UDN15'><tr id='UDN15'><dt id='UDN15'><q id='UDN15'><span id='UDN15'><b id='UDN15'><form id='UDN15'><ins id='UDN15'></ins><ul id='UDN15'></ul><sub id='UDN15'></sub></form><legend id='UDN15'></legend><bdo id='UDN15'><pre id='UDN15'><center id='UDN15'></center></pre></bdo></b><th id='UDN15'></th></span></q></dt></tr></i><div id='UDN15'><tfoot id='UDN15'></tfoot><dl id='UDN15'><fieldset id='UDN15'></fieldset></dl></div>

    1. <tfoot id='UDN15'></tfoot>
        <bdo id='UDN15'></bdo><ul id='UDN15'></ul>

      1. <legend id='UDN15'><style id='UDN15'><dir id='UDN15'><q id='UDN15'></q></dir></style></legend>

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

        SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data?(SyntaxError:JSON.parse:JSON 数据的第 1 行第 1 列出现意外字符?)

          1. <small id='h9xJb'></small><noframes id='h9xJb'>

            <tfoot id='h9xJb'></tfoot>
                <tbody id='h9xJb'></tbody>
            • <i id='h9xJb'><tr id='h9xJb'><dt id='h9xJb'><q id='h9xJb'><span id='h9xJb'><b id='h9xJb'><form id='h9xJb'><ins id='h9xJb'></ins><ul id='h9xJb'></ul><sub id='h9xJb'></sub></form><legend id='h9xJb'></legend><bdo id='h9xJb'><pre id='h9xJb'><center id='h9xJb'></center></pre></bdo></b><th id='h9xJb'></th></span></q></dt></tr></i><div id='h9xJb'><tfoot id='h9xJb'></tfoot><dl id='h9xJb'><fieldset id='h9xJb'></fieldset></dl></div>

              <legend id='h9xJb'><style id='h9xJb'><dir id='h9xJb'><q id='h9xJb'></q></dir></style></legend>
                <bdo id='h9xJb'></bdo><ul id='h9xJb'></ul>

                  本文介绍了SyntaxError:JSON.parse:JSON 数据的第 1 行第 1 列出现意外字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  谁能告诉我为什么当我从我的硬盘访问数据时这段代码可以在 HTML 页面中完美运行,但是当我将它添加到 express 和节点时我得到一个

                  Can anyone tell me why this code will work perfectly in an HTML page when accessing the data from my hard drive but when I add it to express and node I get a

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

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

                  with perfectly formatted code. I know I tested it with a formatter and I even manually created a json object. Here is the code:

                  <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- same exact file that worked perfectly before ran thru express now causes this error: 
                  **"index.ejs"**
                  
                      <div id="output"></div>
                  
                            <button id="getProperty">Get Property</button>
                  
                      <script>
                  
                              document.getElementById('getProperty').addEventListener('click', getProperty);
                  
                  
                              function getProperty() {
                                  /*fetch('sample.txt')
                                  .then(function(res) {
                                      return res.text();
                                  })
                                  .then(function(data){
                                      console.log(data);
                                  });*/
                                  fetch("2016-regular.json")
                                  .then((res) => res.json())
                                  .then((data) => {
                                      console.log(data);//**won't even read the data without that error**
                                  });
                              }
                      </script>
                  
                  
                  **express code**
                  var express = require('express');
                  //var bodyParser = require('body-parser');
                  var cors = require('cors');
                  var path = require('path');
                  
                  var app = express();
                  
                  app.use(bodyParser());
                  app.use(cors());
                  
                  app.set('views', path.join(__dirname, 'views'));
                  app.set('view engine', 'ejs');
                  
                  app.get('/', function (request, response) {
                      response.render('index.ejs');
                  });
                  
                  app.listen(8000, function() {
                      console.log('running on 8000');
                  });
                  

                  任何想法为什么在访问文件夹时可以在纯 html 中正常工作,或者如果我手动创建文件并将其保存在硬盘驱动器上,但是一旦我将其放入 express 或尝试访问数据来自的 API(最终目标) 我收到错误 SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

                  any ideas why this works fine in plain html when accessing a folder or if I manually create and save the file on my hard drive but once I put it in express or try to access the API the data came from (the final goal) I get the error SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

                  推荐答案

                  我认为底线是您需要确保您的 JSON 文件是可访问/可加载的,然后确保它是有效的.这是一个最小的工作示例.你的 app.js 应该很简单:

                  I think the bottom line is that you need to make sure your JSON file is reachable/loadable, and then make sure it is valid. Here is a minimal working example. You app.js should be simply:

                  var express = require('express');
                  var index = require('./routes/index');
                  var app = express();
                  
                  app.set('views', 'views');
                  app.set('view engine', 'ejs');
                  
                  app.use(express.static('public'));
                  
                  app.use('/', index);
                  
                  module.exports = app;
                  

                  你的 routes/index.js 很简单:

                  Your routes/index.js is simply:

                  var express = require('express');
                  var router = express.Router();
                  
                  /* GET home page. */
                  router.get('/', function(req, res, next) {
                    res.render('index', { title: 'Express' });
                  });
                  
                  module.exports = router;
                  

                  你的views/index.ejs应该是:

                  And your views/index.ejs should be:

                  <html>
                  <body>
                    <div id="output">JSON contents will appear here.</div>  
                    <button id="getProperty">Click to load JSON</button>
                  
                    <script>
                        document.getElementById('getProperty').addEventListener('click', getProperty);  
                        function getProperty() {
                            fetch("./2016-regular.json")
                            .then((res) => res.json())
                            .then((data) => {
                                document.getElementById('output').innerHTML = JSON.stringify(data);
                            });
                        }
                    </script>
                  </body>
                  </html>     
                  

                  最后,确保您的 JSON 文件保存在 ./public/2016-regular.json 中.这是我的:

                  Finally, make sure your JSON file is saved at ./public/2016-regular.json. Here is mine:

                  {项目1":价值1",项目2":价值2",项目 3":价值 3"}

                  { "item1": "value1", "item2": "value2", "item3": "value3" }

                  测试 1 确保您的 JSON 文件可访问,我将您的浏览器指向 http://localhost:3000/2016-regular.json(请注意,如果您在不同的端口上运行,您可能需要更改端口).

                  TEST 1 Make sure your JSON file is reachable my pointing your browser to http://localhost:3000/2016-regular.json (note you may have to change the port if you are running on a different port).

                  测试 2 导航到 http://localhost:3000/ 并点击按钮.文件内容应该出现在结果 div 中.

                  TEST 2 Navigate to http://localhost:3000/ and click on the button. The file contents should appear in the results div.

                  完整的工作代码可在此处获得.只需克隆存储库,然后

                  Full working code is available here. Just clone the repository, then

                  cd exp1
                  npm install
                  npm start
                  

                  这篇关于SyntaxError:JSON.parse:JSON 数据的第 1 行第 1 列出现意外字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))
                  <legend id='Yz28j'><style id='Yz28j'><dir id='Yz28j'><q id='Yz28j'></q></dir></style></legend>

                  <i id='Yz28j'><tr id='Yz28j'><dt id='Yz28j'><q id='Yz28j'><span id='Yz28j'><b id='Yz28j'><form id='Yz28j'><ins id='Yz28j'></ins><ul id='Yz28j'></ul><sub id='Yz28j'></sub></form><legend id='Yz28j'></legend><bdo id='Yz28j'><pre id='Yz28j'><center id='Yz28j'></center></pre></bdo></b><th id='Yz28j'></th></span></q></dt></tr></i><div id='Yz28j'><tfoot id='Yz28j'></tfoot><dl id='Yz28j'><fieldset id='Yz28j'></fieldset></dl></div>
                    <bdo id='Yz28j'></bdo><ul id='Yz28j'></ul>
                    <tfoot id='Yz28j'></tfoot>
                      <tbody id='Yz28j'></tbody>

                          <small id='Yz28j'></small><noframes id='Yz28j'>