1

fetch的兼容

import 'fetch-detector' //fetch的兼容包

import 'fetch-ie8'

Fetch的post请求

const fetchPost = (url,params)=>{

 return fetch(url,{

 method:"POST",

 header:{

 "Content-Type":"application/x-www-form-urlencode"

 },

 params:params,

 credentials:'include',//设置了这个之后 请求才会带上cookie

 }).then(res=>{

 if(!res.OK){

 throw Error(res.statusText)

 }

 return res.json()

 })

}

export {fetchPost}

Fetch mock数据



import FetchMock from 'fetch-mock'

  

FetchMock.mock('/login',(url,opts)=>{

 const params = opts.params

 if(params.account === '17521077157'){

 if(params.password === '12345'){

  

 }else{

 return {

 code:401,message:'密码错误'

 }

 }

 }else{

 return {code:400,message:'用户名错误'}

 }

}

  

)

HappyCodingTop
526 声望847 粉丝

Talk is cheap, show the code!!