在過去的幾天里,我一直在嘗試解決這個錯誤。 我非常困惑,因為我不知道為什么這不起作用。 任何幫助或建議將不勝感激。
當我嘗試在我的客戶端 js 中從 node.js 獲取數據時,我的 JSON 解析器給我一個錯誤:
uncaught SyntaxError: Unexpected token '<', "<.DOCTYPE "... is not valid JSON at JSON.parse () at XMLHttpRequest.onLoad (something:js:18:29)
為此,我使用了 node、js 和 html,總共 3 個文件。 這是 index.js:
const express = require('express'); const app = express(); app.use(express.static('public')); app.use(express.json()); app.listen(3000, () => { console.log('server started'); }); app.get('/', function(req, res) { console.log('hello') }); app.listen(8080); var bodyParser = require('body-parser'); app.use(bodyParser.urlencoded({ extended: false })); app.use(bodyParser.json()); app.post('/testing/thisshit', async function(req, res) { console.log(req.body); var dataToSendToClient = '{"message":"potatohead"}'; res.send(dataToSendToClient); });
html:
hellow world