[小程序代码片段] 封装ajax请求

request.js

// 封装请求
  const  ajax = (url,method,data)=>{
    
    
  // 显示loading 
   wx.showLoading({
    
    
     title: '加载中',
   })
   return new Promise((resolve,reject)=>{
    
    
     wx.request({
    
    
       url: url, //仅为示例,并非真实的接口地址
       data:data,
       method:method,
       header: {
    
    
         'content-type': 'application/json' // 默认值
       },
       success (res) {
    
    
         // console.log(res.data)
         resolve(res.data)
         // 隐藏loading
         wx.hideLoading()
       },
       fail(err){
    
    
         reject(err)
       }
     })
   })
}

export default ajax

http.js

// 管理所有的请求
import ajax from './request'

let base_url = 'https://mpapi.iynn.cn/api/v1/'
// 首页热点新闻接口
export function GetnewsHot(data){
    
    
  return ajax(base_url+'news/hot','get',data)
}

猜你喜欢

转载自blog.csdn.net/qq_14993591/article/details/121532171