我试图使用fetch发送一个数组,如下所示:
{"cards":[[189,2],[211,2],[238,2],[778,2],[985,2],[1008,2],[1073,2],[1171,2],[48886,2],[49161,2],[49164,2],[49184,1],[49356,2],[50372,2],[51722,1],[52422,2]],"heroes":[1066],"format":2}以下是我所尝试的:
getCardsForDeck = deck => {
var stringifiedDeck = JSON.stringify(deck);
console.log("stringifiedDeck:" + stringifiedDeck);
fetch(`http://localhost:3001/api/getCardsForDeck`, {
method: "PUT",
body: stringifiedDeck
})
.then(cards => cards.json())
.then(res => this.setState({ cards: res.cards }));
};不过,我收到了一个错误:
Unhandled Rejection (SyntaxError): Unexpected token < in JSON at position 0如果没有使用JSON.stringify(),如何发送这些数据?还是必须编辑数据以删除括号?
在检查网络选项卡中的api时,它给出了以下内容:
SyntaxError: Unexpected token o in JSON at position 1
[0] at JSON.parse (<anonymous>)
[0] at parse (C:\Users\User\projects\webapp\deck-editor\backend\node_modules\body-parser\lib\types\json.js:89:19)
[0] at C:\Users\User\projects\webapp\deck-editor\backend\node_modules\body-parser\lib\read.js:121:18
[0] at invokeCallback (C:\Users\User\projects\webapp\deck-editor\backend\node_modules\raw-body\index.js:224:16)
[0] at done (C:\Users\User\projects\webapp\deck-editor\backend\node_modules\raw-body\index.js:213:7)
[0] at IncomingMessage.onEnd (C:\Users\User\projects\webapp\deck-editor\backend\node_modules\raw-body\index.js:273:7)
[0] at IncomingMessage.emit (events.js:194:15)
[0] at endReadableNT (_stream_readable.js:1125:12)
[0] at process._tickCallback (internal/process/next_tick.js:63:19)
[0] SyntaxError: Unexpected token o in JSON at position 1
[0] at JSON.parse (<anonymous>)
[0] at parse (C:\Users\User\projects\webapp\deck-editor\backend\node_modules\body-parser\lib\types\json.js:89:19)
[0] at C:\Users\User\projects\webapp\deck-editor\backend\node_modules\body-parser\lib\read.js:121:18
[0] at invokeCallback (C:\Users\User\projects\webapp\deck-editor\backend\node_modules\raw-body\index.js:224:16)
[0] at done (C:\Users\User\projects\webapp\deck-editor\backend\node_modules\raw-body\index.js:213:7)
[0] at IncomingMessage.onEnd (C:\Users\User\projects\webapp\deck-editor\backend\node_modules\raw-body\index.js:273:7)
[0] at IncomingMessage.emit (events.js:194:15)
[0] at endReadableNT (_stream_readable.js:1125:12)
[0] at process._tickCallback (internal/process/next_tick.js:63:19)发布于 2019-05-31 13:31:13
您似乎期望http://localhost:3001/api/getCardsForDeck将JSON 发送回,然后将JSON 发送给。很可能是用HTML响应,而不是JSON。
该代码中还有另外两个错误:
http://localhost:3001/api/getCardsForDeck,{方法:"PUT",主体: stringifiedDeck,标题:{ // *“内容-类型”:"application/json“// **} // * })ok标志:
fetch(http://localhost:3001/api/getCardsForDeck,{ method:"PUT",body: stringifiedDeck,headers:{“Content”:"application/json“} }) .then(响应=> { if (!response.ok) { // *)抛出新错误(”HTTP“+ response.status);// *} // * // ...use response.json,response.text等) .catch(error => { // .handle/report error });发布于 2022-06-21 14:25:29
我今天遇到了这个问题,但我想把列表转移到后端,而不是接收正确的错误。解决办法真的很简单,我不敢相信我坚持了几个小时。
我的解决办法如下:
const response = await fetch(SERVER_URL + `/api/studyPlan/${plan.userid}`, {
method: "POST",
headers: { "Content-Type": "application/json" },
credentials: "include",
body: JSON.stringify({
plan: planlist,
}),
});列表中的planlist是数组。我们可以将这个列表作为值发送到后端。然后在后端,直接使用req.body.plan获取信息。
希望它能帮上忙
https://stackoverflow.com/questions/56395941
复制相似问题