[トラブルシューティング][Express]未キャプチャ(in promise)SyntaxError:Unexpected token<in JSON at position 0
7259 ワード

Uncaught (in promise) SyntaxError: Unexpected token < in JSON at position 0
非同期通信の間にエラーが発生しました.
express()ドキュメントを見て

strictのdefalut値はtrueであり、この状態では配列とオブジェクトのみが許可されていることがわかります.
ただし、文字列を受信する必要があるため、strict propertyの値をfalseに変換する必要があります.
express.json([options])
メソッドを使用します.optionsはオブジェクトタイプです.
以下の表に記入すれば解決します.
app.use(express.json({ strict: false }));
const port = 4999;
const ip = 'localhost';
const express = require('express');
const cors = require('cors')
const app = express();
app.use(cors());
app.use(express.json({ strict: false }));
app.use(express.static('client'));
app.get('/', (req, res) => {
res.send('Hello World!');
})
app.post('/upper', (req, res) => {
let result = req.body;
res.json(result.toUpperCase())
})
app.post('/lower', (req, res) => {
let result = req.body;
res.json(result.toLowerCase())
})
app.listen(port, () => {
console.log(`server listening on ${ip}:${port}`);
})
Reference
この問題について([トラブルシューティング][Express]未キャプチャ(in promise)SyntaxError:Unexpected token<in JSON at position 0), 我々は、より多くの情報をここで見つけました https://velog.io/@tyoon225/문제해결Express-Uncaught-in-promise-SyntaxError-Unexpected-token-in-JSON-at-position-0テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol