小程序封装Promise去请求数据,可是服务器报错
发布于 6 年前 作者 gang41 13564 次浏览 来自 官方Issues

就报这种错误 我封装的代码如下

onst Config = require(’./config.js’) //导入自定义配置文件

const http = ({

url = ‘’, //请求地址

param = {}, //请求参数

contentType, //请求头

method = “GET”, //请求方式

isPermissions = false //是否认证

} = {}) => {

//设置请求头

let header = {

“content-type”: contentType ? contentType : method == ‘POST’ ? ‘application/x-www-form-urlencoded’ : ‘application/json’,

}

if (isPermissions) {

//添加token到header中

header.token = wx.getStorageSync(‘token’)

}

let timeStart = Date.now(); //开始请求时间

return new Promise((resolve, reject) => {

wx.request({

url: getUrl(url),

data: param,

header: header,

method: method,

complete: (res) => {

console.log(`请求地址:${getUrl(url)}`);

console.log(`请求参数:${JSON.stringify(param)}`);

console.log(`耗时${Date.now() - timeStart}ms`);

if (res.statusCode >= 200 && res.statusCode < 300) {

resolve(res)

} else if (res.statusCode === 500) {

console.log(res)

// _login()

reject(res)

} else {

reject(res)

}

}

})

})

}

//获取请求地址

const getUrl = (url) => {

if (url.indexOf(’://’) == -1) {

url = Config.BaseUrl + url;

}

return url

}

// get方法

const _get = (url, param = {}, isPermissions) => {

return http({

url,

param,

isPermissions

})

}

const _post = (url, param = {}, isPermissions, contentType) => {

return http({

url,

param,

method: ‘POST’,

isPermissions,

contentType

})

}

}

module.exports = {

_get,

_post

}

我请求的接口是这样的:

const netWorke = require(’./utils/network2.js’) //network2.js就是封装的Promise请求

netWorke._get(‘wxorder/GetMerchant’, {}, false).then(res => {

console.log(res)

})

有大神能帮帮我不

2 回复

500一般是后台代码的问题

这种错,你得看服务器那边的log,看客户端返回的数据是看不出来的。

你对比一下有效请求和你封装后的请求头有什么区别,看看能不能发现问题。

回到顶部