Advertisement

React学习内容--发送请求方法(axios与fetch)

阅读量:

前端项目发送请求的方式

(1)jQuery—$.get 容易陷入回调地狱

(2)axios—axios.get 采用 Promise 风格进行请求

上述两种方式均依赖于 xhr,若缺少 xhr 则无法发起请求,其本质仍属于 ajax 请求

(3)fetch:作为系统原生支持的功能,可直接发送请求,无需依赖 xhr,同时也遵循 Promise 风格

axios请求发送机制

复制代码
    search = async () =>{
    	const {value} = this.keyWordNode
    // http://api.github.com/search/users?q=xxxxx
    // 发送请求前更新状态
    	this.props.updateAppState({isFirst:false,isLoading: true})
    PubSub.publish('msg',{isFirst:false,isLoading: true})
    	axios.get(`/api1/search/users2?q=${keyWord}`).then(
    			response => {
    				//请求成功后通知List更新状态
    				PubSub.publish('atguig

全部评论 (0)

还没有任何评论哟~