版權聲明:本文為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