版权声明:本文为CSDN博主「小歪 | 前端」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44599931/article/details/113933940
————————————————
参数已经提交给后端了,但是后端获取接受不了参数
解决方法:修改请求头Content-type
header:{ 'Content-type':'application/x-www-form-urlencoded' } //或者: header : { 'content-type': 'application/x-www-form-urlencoded;charset=utf-8' }
uni.request({ url: 'https://www.example.com/request', //仅为示例,并非真实接口地址。 data: { text: 'uni.request' }, success: (res) => { console.log(res.data); this.text = 'request success'; } });
原因:请求的 header 中 content-type 默认为 application/json。
data 数据说明
最终发送给服务器的数据是 String 类型,如果传入的 data 不是 String 类型,会被转换成 String。转换规则如下:
对于 GET 方法,会将数据转换为 query string。例如 { name: 'name', age: 18 } 转换后的结果是 name=name&age=18。
对于 POST 方法且 header['content-type'] 为 application/json 的数据,会进行 JSON 序列化。
对于 POST 方法且 header['content-type'] 为 application/x-www-form-urlencoded 的数据,会将数据转换为 query string