第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Javascript fetch() 沒有從本地主機(jī)返回預(yù)期的 json

Javascript fetch() 沒有從本地主機(jī)返回預(yù)期的 json

墨色風(fēng)雨 2023-09-07 17:04:30
我有一個(gè)提供JSON數(shù)據(jù)的Django后端。當(dāng)我跑步時(shí),我得到:curl 127.0.0.1:8000/posts/[{"title": "This is a title","body": "Body :)","pub_date":"2020-11-25T13:36:57Z"},...]但是,當(dāng)我運(yùn)行此js代碼時(shí)const API = '127.0.0.1:8000/posts/'fetch(API).then(response => console.log(response))我得到:Response { type: "basic", url: "http://localhost:3000/127.0.0.1:8000/posts/", redirected: false, status: 200, ok: true, statusText: "OK", headers: Headers, body: ReadableStream, bodyUsed: false}這是意料之中的。如果我然后嘗試運(yùn)行,我會(huì)得到.then(response => response.json())Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data當(dāng)我跑步時(shí)fetch(API).then(response => console.log(response.headers))fetch(API).then(response => console.log(response.text()))我得到Headers {  }Promise { "pending "}   <state>: "pending"分別此外fetch(API).then(response => console.log(response.text()))fetch(API).then(response => response.json()).then(data => console.log(data))只是發(fā)回Uncaught (in promise) SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data錯(cuò)誤更新:我還注意到在 Django 服務(wù)器日志上,當(dāng)我刷新 javascript 頁面時(shí)沒有出現(xiàn)新的請(qǐng)求。但是,當(dāng)我運(yùn)行 curl 時(shí),有一個(gè) GET 請(qǐng)求。
查看完整描述

3 回答

?
慕運(yùn)維8079593

TA貢獻(xiàn)1876條經(jīng)驗(yàn) 獲得超5個(gè)贊

您需要將響應(yīng)更新為 json,如下所示:

const API = '127.0.0.1:8000/posts/';
fetch(API)
    .then(response => response.json())
    .then(response => console.log(response));


查看完整回答
反對(duì) 回復(fù) 2023-09-07
?
慕村9548890

TA貢獻(xiàn)1884條經(jīng)驗(yàn) 獲得超4個(gè)贊

我認(rèn)為你應(yīng)該使用JSON.parse(response);

fetch(myRequest)
  .then(response => response.json())
  .then(data => {console.log( data })


查看完整回答
反對(duì) 回復(fù) 2023-09-07
?
交互式愛情

TA貢獻(xiàn)1712條經(jīng)驗(yàn) 獲得超3個(gè)贊

如果后端有任何錯(cuò)誤日志,您可以先進(jìn)行,因?yàn)槿绻诓煌亩丝谏线\(yùn)行前端和后端,因此,首先檢查是否有任何錯(cuò)誤。您可能會(huì)遇到 CORS 錯(cuò)誤。

然后,您可以嘗試使用

const API = '127.0.0.1:8000/posts/';
fetch(API)
    .then(response => response.json())
    .then(response => console.log(response));


查看完整回答
反對(duì) 回復(fù) 2023-09-07
  • 3 回答
  • 0 關(guān)注
  • 171 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)