第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

帶有空格的 Axios GET 請求參數(shù)

帶有空格的 Axios GET 請求參數(shù)

白衣非少年 2021-11-12 17:13:39
目標我想使用axios. 參數(shù)值是一個字符串類型的變量并且有空格。問題似乎axios是以我的后端不理解的格式對參數(shù)進行編碼。我已經(jīng)對axios編碼進行了研究,它似乎axios將空格編碼為 a+而不是%20。例子假設您有以下請求: const whitespace = "white space"; const encodeWhitespace = encodeURI(whitespace); const noSpace = "no"; axios.get('/api', {   params: {     'foo': 'bar',     'bar': 'hello world',     'space': whitespace,     'encode': 'hello%20world',      'encoded': encodeWhitespace,     'simple': noSpace   }}這些參數(shù)foo, bar, encode, simple都可以工作并使用正確的數(shù)據(jù)生成響應。參數(shù)space, encoded不會生成正確的數(shù)據(jù)。請求以 200 成功,但不返回任何數(shù)據(jù)。我使用相同的查詢在 Postman 中創(chuàng)建了相同的請求,以查看是否GET返回了預期的結果,并且確實如此。我%20在 Postman 中添加了它,它返回得很好。我+在 Postman 中添加了它,它也返回了預期的結果。題變量實現(xiàn)可能會出什么問題?如果沒有像barparam這樣的變量,我就無法做到這一點,因為該值正在傳遞給一個函數(shù)(Redux 操作)。對此的任何想法或想法都會有所幫助。如果需要更多信息,請發(fā)表評論。
查看完整描述

1 回答

?
有只小跳蛙

TA貢獻1824條經(jīng)驗 獲得超8個贊

似乎這是Axios 庫的一個問題(或默認參數(shù)序列化行為)。


因此,要克服這一點,您有 2 個選擇。


在 URL 本身中定義您的查詢參數(shù)

const whitespace = "white space";

axios.get(`/api?space=${whitespace}`);

自己編寫paramsSerializer以構建查詢字符串。

const whitespace = "white space";

const encodeWhitespace = encodeURI(whitespace);

const noSpace = "no";


axios.get('/api', {

    params: {

        'foo': 'bar',

        'bar': 'hello world',

        'space': whitespace,

        'simple': noSpace

    },

    paramsSerializer: (params) => {

        // Sample implementation of query string building

        let result = '';

        Object.keys(params).forEach(key => {

            result += `${key}=${encodeURIComponent(params[key])}&`;

        });

        return result.substr(0, result.length - 1);

    }

});

注意:以上paramsSerializer也可以定義在全局級別或 Axios 實例級別。


全球層面

axios.defaults.paramsSerializer = (params) => { /* ... */ };

實例級別

let axInstance = axios.create({ paramsSerializer: (params) => { /* ... */ } })


查看完整回答
反對 回復 2021-11-12
  • 1 回答
  • 0 關注
  • 701 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號