1.頁面根目錄下新建api 文件夾,作用發(fā)起請(qǐng)求
2. api文件夾下建一個(gè)http.js 用來發(fā)送promise請(qǐng)求
import baseUrl from "./baseUrl"
var fun = function (config) {
return new Promise((resolve, reject) => {
wx.request({
//url: 'url',//路徑
url: baseUrl + config.url,
// timeout: 5000, //設(shè)置請(qǐng)求時(shí)間
// data: {
// // 存放的數(shù)據(jù)
// },
// data:config.data,
method:config.method,
// method: "GET", //數(shù)據(jù)請(qǐng)求方式
// header:"", 請(qǐng)求頭設(shè)置
success: res => {
resolve(res)
console.log('res數(shù)據(jù)成功了', res);
}
// fail(err) {
// console.log('err數(shù)據(jù)請(qǐng)求失敗了', err);
// },
// complete: res => {
// console.log("請(qǐng)求完成后的參數(shù)");
// }
})
})
}
export default fun
http.sj 同級(jí)新建js文件 baseUrl ,用來設(shè)置共同的請(qǐng)求頭
export default "https://autumnfish.cn"
api 下新建 index文件夾 文件夾下 設(shè)置 index.js文件
import http from "../http"
function getList(data) {
return http({
url: '/top/artists',// 放入除了baseUrl 的路徑
data ,//參數(shù),
method: "GET",
})
}
export {getList}
在需要用到的地方
導(dǎo)入
import {
getList
}
from "../../api/index/index"
onLoad(options) {
getList({里面放請(qǐng)求的參數(shù)}).then(res => console.log(res.data.artists))
},