关于javascript:Fetch在项目中灵活使用

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:'用户名谬误'}

 }

}

  

)

评论

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理