fetch用法說明

2022-05-10 12:39:27 字數 2247 閱讀 3174

fetch(url, options).then(function(response) , function(error) )

具體引數案例:

//相容包

require('babel-polyfill')

require('es6-promise').polyfill()

import 'whatwg-fetch'

fetch(url, ,

credentials: "same-origin"

}).then(function(response) )

}, function(error) )

url

定義要獲取的資源。這可能是:

options(可選)

乙個配置項物件,包括所有對請求的設定。可選的引數有:

response

方法:其他方法:

response.headersget請求

使用 es6 的 箭頭函式 後:

fetch(url)

.then(response => response.json())

.then(data => console.log(data))

.catch(e => console.log("oops, error", e))

response的資料

fetch('/users.json').then(function(response) )

post請求
fetch('/users', ,

body: json.stringify()

})

檢查請求狀態

function checkstatus(response)  else 

}function parsejson(response)

fetch('/users')

.then(checkstatus)

.then(parsejson)

.then(function(data) ).catch(function(error) )

promise 物件是乙個返回值的**,這個返回值在promise物件建立時未必已知。它允許你為非同步操作的成功或失敗指定處理方法。 這使得非同步方法可以像同步方法那樣返回值:非同步方法會返回乙個包含了原返回值的 promise 物件來替代原返回值。

promise建構函式接受乙個函式作為引數,該函式的兩個引數分別是resolve方法和reject方法。如果非同步操作成功,則用resolve方法將promise物件的狀態變為「成功」(即從pending變為resolved);如果非同步操作失敗,則用reject方法將狀態變為「失敗」(即從pending變為rejected)。

promise例項生成以後,可以用then方法分別指定resolve方法和reject方法的**函式。

//建立乙個promise物件

var promise = new promise(function(resolve, reject) else

});//then方法可以接受兩個**函式作為引數。

//第乙個**函式是promise物件的狀態變為resolved時呼叫,第二個**函式是promise物件的狀態變為reject時呼叫。

//其中,第二個函式是可選的,不一定要提供。這兩個函式都接受promise物件傳出的值作為引數。

promise.then(function(value) , function(value) );

那麼結合promise後fetch的用法:

export function fetch(url, options) )

.then(data => else else

reject(data) //返回失敗資料}})

.catch(error => )

})return defer

}

呼叫fech方法:

import  from './fetch'

fetch(getapi('search'), )

.then(data => )

C new 用法說明

int a newint 這句相當於 int a 0 a 是空 int a new int 20 這句的本質是指標指向乙個 int20 的空間 在堆裡 這個與class 類似的。1 new運算子 用於建立物件和呼叫建構函式。這種大家都比較熟悉,沒什麼好說的了。2 new修飾符 在用作修飾符時,new...

SQLSERVER DBCC 用法說明

一 dbcc 幫助類命令 dbcc help 查詢所有的dbcc命令 dbcc help 命令 查詢指定的dbcc命令的語法說明 dbcc useroptions 返回當前連線的活動 設定 的set選項 二 dbcc 檢查驗證類命令 dbcc checkalloc 資料庫名稱 檢查指定資料庫的磁碟空...

linux c setitimer用法說明

在linux c程式設計中,setitimer是乙個比較常用的函式,可用來實現延時和定時的功能。使用時需要引入的標頭檔案 cpp view plain copy include setitimer函式原型 cpp view plain copy intsetitimer intwhich,const...