fetch的基本应用

时间:2019-06-16
本文章向大家介绍fetch的基本应用,主要包括fetch的基本应用使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。


fetch
浏览器自带的一种前后端数据处理的方式

cnpm install whatwg-fetch --save


fetch(url,options).then((res)=>res.json()).then((data)=>{})

特点:第一次返回的结构不是response 而是一个未处理的response集合

fetch也是基于promise的


axios(url).then(()=>)

fetch(url,options)

options:
method:请求的方式
header:请求头
body:当post请求的时候需要传递参数的地方(将传递的对象转换成字符串形式的) 利用一下qs 第三方库


fetch默认是不会携带Cookie的 如果想要设置cookie 需要在headers中设置credentials:include


react中如何进行跨域

异步action
applyMiddleware:使用中间件处理异步的action

常用处理异步action的方式
redux-promise-middleware

1。引入
import reduxPromise from "redux-promise-middleware";

2、使用中间件

const store = createStore(reducer,applyMiddleware(reduxPromise));

3、处理异步的action
异步的action必须要在actionCreator中处理

type:"GET_MOVIE",
payload:new Promise(resolve=>{
//异步
fetchPolyfill("/ajax/movieOnInfoList?token=")
//未处理的结果集
.then((res)=>res.json())
.then((data)=>{
resolve(data);
})

})

redux-thunk

redux-saga

dva

在React中什么叫做中间件?
action派发后state立马做出修改 这叫同步
action派发后state不知道什么时候做修改 这个其实就是异步


中间件:请求和回复之间的一个应用

React: action与reducer之间的一个应用

原文地址:https://www.cnblogs.com/PeiGaGa/p/11032727.html