road-http
v1.0.0
Published
一个网络请求封装
Downloads
3
Readme
对javascript的ajax的封装
import road from "road"
//默认使用get请求
road("/")
.setData({})
.request((res)=>{
console.log(res)
})
//也可以调用get方法发起请求,可以这样调用 put 、post、delete等请求方法
road.get("/")
.setHeader({
"Content-type":"application/json"
})
.request((res)=>{
console.log(res)
})
// 也可以使用object来调用
road({
type:"GET",
header:{
"COntent-type":"application/json"
},
data:{}, //需要发送到服务器的数据
actionType:"REQUEST_SUCCESS", // 在redux环境下可以设定actionType来将请求数据保存到store中
success:(res)=>{},
error:(res)=>{}, //请求错误时的调用函数 ,不定义将使用setDefaultError定义的默认错误处理函数
}).request()
// 可以通过设置delay来启动轮询请求
let loop = road.get("/")
loop.setDelay(1000) //设置轮询的请求间隔
loop.request((res)=>{
console.log(res)
})
// 调用cancelLoop 取消轮询
loop.cancelLoop()