【轉】uni-app請求post接口后端獲取不到參數


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM