我正在尝试使用如下所示的 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)
最佳答案
您似乎期望 http://localhost:3001/api/getCardsForDeck
在将 JSON 发送给 之后发送 JSON .它很可能使用 HTML 而不是 JSON 进行响应。
该代码中还有另外两个错误:
您需要说出您要发送的内容,因为它不是默认的 URI 编码数据:
fetch(`http://localhost:3001/api/getCardsForDeck`, { method: "PUT", body: stringifiedDeck, headers: { // *** "Content-Type": "application/json" // *** } // *** })
您需要检查响应的状态。 (每个人 都忽略了这一点。)最简单的方法是检查响应中的
ok
标志:fetch(`http://localhost:3001/api/getCardsForDeck`, { method: "PUT", body: stringifiedDeck, headers: { "Content-Type": "application/json" } }) .then(response => { if (!response.ok) { // *** throw new Error("HTTP error " + response.status); // *** } // *** // ...use `response.json`, `response.text`, etc. here }) .catch(error => { // ...handle/report error });
关于javascript - 如何使用 fetch 发送数组? (JavaScript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56395941/