uniapp request:fail invalid url “”"
我把uniapp请求接口分装了一下,为了考虑很多的场景,分装的稍微和平常见的有点差别,
import serviceUrl from '../server.js'
function params(json) {
if (!json) return ''
return cleanArray(
Object.keys(json).map(key => {
if (json[key] === undefined) return ''
return encodeURIComponent(key) + '=' + encodeURIComponent(json[key])
})
).join('&')
}
const request = (options) => {
return new Promise((resolve, reject) => {
uni.request({
// 这里的三目运算,之前没加()所以报的这个错
url: serviceUrl+ options.url + (options.params ? params(options.params):''),
method: options.method || 'GET',
data: options.data || '{}',
header: {
'Content-Type': 'application/json',
'X-Dts-Token': uni.getStorageSync('token')
},
success: (res) => {
if (res.statusCode == 200 || res.statusCode == 201) {
resolve(res)
} else {
uni.showToast({
icon: "none",
title: "请求接口失败"
})
}
},
fail: (err) => {
uni.showToast({
icon: "none",
title: "请求接口失败"
})
reject(err)
}
})
})
}
export default request
请求api.js
import request from '../../until/request.js'
// 微信登录
export function wxlogin(data) {
return request({
url: '/api/wxlogin/login',
method: 'post',
data
})
}
调用方式
import { wxlogin } from '../../api/commont/login.js'
loginFun(res){
wxlogin(this.loginForm).then((respone)=>{
console.log(respone)
})
},