catta
1.0.0
catta 是一個簡單的瀏覽器請求客戶端,支援 Fetch、AJAX、JSONP 甚至自訂您自己的適配器。 中文文檔-請點我
catta-min.jscatta-min-comp.js{data: HTMLFormElement}需要 IE 10+ 才能運作如果你的專案已經有這些polyfill或不需要IE支持,建議使用純的。
# local install
npm install catta --save // With ES6 - *Recommend*
import catta from 'catta' ;
catta ( 'http://some/url' ) . then ( function ( res ) {
console . log ( res ) ;
} ) ; // With CommonJS
const catta = require ( 'catta' ) ;
// or catta.ajax or catta.jsonp or catta.fetch
catta . default ( 'http://some/url' ) . then ( function ( res ) {
console . log ( res ) ;
} ) ; <!-- And also with <script> in HTML - *Not Recommend* -->
< script src =" ./node_modules/catta/dist/catta-min.js " > </ script >
< script >
// or catta.ajax or catta.jsonp or catta.fetch
catta . default ( 'http://some/url' ) . then ( function ( res ) {
console . log ( res ) ;
} ) ;
</ script > import { ajax , fetch , jsonp , getScript } from 'catta' ;
/**
* make fetch/ajax/jsonp/getScript request
* @param {string} url - request url
* @param {Object} options - request options
*
*/
ajax ( url , options ) ;
// fetch request
fetch ( url , options ) ;
// jsonp request
jsonp ( url , options ) ;
// getScript
getScript ( url ) ;| 描述 | 類型 | 預設 | 拿來 | 阿賈克斯 | JSONP | |
|---|---|---|---|---|---|---|
| 網址 | 請求網址 | 細繩 | 無效的 | v | v | v |
| 方法 | 請求方式 | 字串 { 取得、發布、放置、刪除、頭 } | '得到' | v | v | x |
| 數據 | 數據發送到伺服器 | 字串/物件/表單元素[3] | {} | v | v | v |
| 描述 | 類型 | 預設 | 拿來 | 阿賈克斯 | JSONP | |
|---|---|---|---|---|---|---|
| 類型 | 限制請求類型 | 字串 { 取得、ajax、jsonp、腳本 } | '汽車' | — | — | — |
| 暫停 | 幾秒後拋出超時錯誤 | 數位 | 3 | ! [1] | v | ! [1] |
| 結果類型 | 結果類型 | { 文字、json、回應 } | 文字 | v | v | ! [2] |
v支持!部分支持×不支持
Fetch 和 JSONP 請求無法中止,當前逾時只是拋出逾時錯誤
resultType選項不能與 jsonp 一起使用,因為結果必須是可執行的 javascript 程式碼
僅支援具有FormData 功能的表單元素
號
| 財產 | 描述 | 類型 | |
|---|---|---|---|
| jsonp | 回呼名稱 | 設定自訂回調名稱 | 細繩 |
| 拿來 | 叉 | 指示請求是否可以跨域 | 布林值 |
| 阿賈克斯 | - | - | - |
import catta from 'catta' ;
catta ( 'http://some/url' ) . then ( function ( res ) {
console . log ( res ) ;
} ) ; import catta from 'catta' ;
catta ( 'http://some/url' , {
type : 'jsonp' ,
data : {
page : 5 ,
count : 20
} ,
timeout : 2 ,
credential : false ,
cross : false ,
// sp. options
jsonp : {
callbackName : 'myCustomJSONP1'
}
} )
. then ( res => console . log ( res ) )
. catch ( err => console . log ( err ) ) ; import { fetch } from 'catta' ;
// only use fetch
fetch ( 'http://some/url' , {
data : { a : 1 }
} ) . then ( function ( res ) {
console . log ( res ) ;
} ) ; import catta from 'catta' ;
catta ( 'http://some/url' , {
headers : {
'Content-Type' : 'appliction/json'
}
} )
. then ( function ( res ) {
console . log ( res ) ;
} ) ; import { globalConfig } from 'catta' ;
// set global config, it will work for each request
globalConfig ( {
timeout : 10
} ) ;catta訂適配器只是一個對象,具有detector和processor功能。
import { customAdapter } from 'catta' ;
import mtopAdapter from 'catta/lib/custom/mtop' ;
// set mtop adapter
customAdapter ( 'mtop' , mtopAdapter ) ; options.type麻省理工學院許可證