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

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

獲取:POST json數(shù)據(jù)

獲?。篜OST json數(shù)據(jù)

手掌心 2019-08-02 13:03:09
獲?。篜OST json數(shù)據(jù)我試圖發(fā)布一個(gè)JSON對(duì)象去取.根據(jù)我所能理解的,我需要在請(qǐng)求的主體上附加一個(gè)字符串對(duì)象,例如:fetch("/echo/json/",{     headers: {       'Accept': 'application/json',       'Content-Type': 'application/json'     },     method: "POST",     body: JSON.stringify({a: 1, b: 2})}).then(function(res){ console.log(res) }).catch(function(res){ console.log(res) })使用時(shí)jsfiddle‘s json回聲我希望看到我發(fā)送的對(duì)象({a: 1, b: 2})返回,但這并沒(méi)有發(fā)生-ChromeDevTools甚至沒(méi)有將JSON顯示為請(qǐng)求的一部分,這意味著它沒(méi)有被發(fā)送。
查看完整描述

3 回答

?
紅顏莎娜

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

在搜索引擎中,我最后討論了非json使用FETCH發(fā)布數(shù)據(jù)的主題,所以我想添加以下內(nèi)容。

非JSON您不必使用表單數(shù)據(jù)。您可以簡(jiǎn)單地設(shè)置Content-Type頭到application/x-www-form-urlencoded并使用字符串:

fetch('url here', {
    method: 'POST',
    headers: {'Content-Type':'application/x-www-form-urlencoded'}, // this line is important, if this content-type is not set it wont work
    body: 'foo=bar&blah=1'});

另一種方法body字符串,而不是像我上面所做的那樣鍵入它,是使用庫(kù)。例如,stringify功能來(lái)自query-stringqs包裹。因此,如果使用這個(gè),它看起來(lái)就像:

import queryString from 'query-string';fetch('url here', {
    method: 'POST',
    headers: {'Content-Type':'application/x-www-form-urlencoded'}, // this line is important, if this content-type is not set it wont work
    body: queryString.stringify({for:'bar', blah:1}});




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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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