React Native 之 網路請求 fetch

2021-07-26 22:30:21 字數 1231 閱讀 3101

前言

fetch(url, init)

.then

((response) => )

.then

((responsedata) => )

.catch

((error) => )

.done

();

譯註:

下面是乙個最基本的請求,只傳入乙個引數,預設為get方式請求

fetch(url)

.then

((response) => response.json()) // json方式解析,如果是text就是 response.text

() .then

((responsedata) => )

.catch

((error) => )

.done

();

方式一:

fetch(url, 

body:"key1=value&key2=value…&keyn=value"

}).then

((response) => )

.then

((responsedata) => )

.catch

((error) => )

.done

();

方式二:

let formdata = new formdata();

fetch(url, ,

body:formdata,

}).then((response)=>

}).then((json)=>).catch.((error)=>)

譯註:

fetch跨域請求的時候預設是不帶 cookie 的,如果需要進行設定 credentials:』include』。

console.log(response.headers.get('content-type'));

...console.log(response.headers.get('date'));

譯註:

從零學React Native之14 網路請求

通過http或者https協議與網路側伺服器交換資料是移動應用中常見的通訊方式。node fetch是rn推薦的請求方式。react native框架在初始化專案時,引入了node fetch包 因為npm3把依賴全部攤平了,node fetch就在node modules目錄下 下面就是專案中引入...

python網路請求 python之網路請求

get 請求 1 有請求引數的型別 鍵值對形式表示引數 url data 引數 res requests.get url,data text 轉成str型別 print res 2 有請求頭 鍵值對形式表示請求頭 url header res requests.get url,headers hea...

React網路請求fetch之post請求

本節介紹下react下fetch的post請求 1 編寫服務端 載入express模組 var express require express 載入path路徑處理核心模組 var path require path 載入body parser中介軟體 var bodyparser require ...