欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

axios請(qǐng)求參數(shù)的三種方式示例詳解

 更新時(shí)間:2023年07月24日 08:57:03   作者:小氣鬼SYQ  
這篇文章主要介紹了axios請(qǐng)求參數(shù)的三種方式,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下

axios請(qǐng)求參數(shù)的三種方式

1、拼接方式:     ?   &

  1. Query String Parameters(URL參數(shù))
        //          + 請(qǐng)求頭參數(shù) => 參數(shù)被定義在請(qǐng)求頭的 請(qǐng)求地址中
        //          + 該參數(shù)的攜帶方式主要被應(yīng)用于無請(qǐng)求體的請(qǐng)求中
        //          + http|https協(xié)議的組成中,以?關(guān)鍵字分割地址和參數(shù)
        //          + 參數(shù)由 key=value&key=value…… 字符串格式進(jìn)行組成
        //              - key 參數(shù)屬性名(該屬性名由服務(wù)器段決定)
        //              - value 參數(shù)屬性值(理論上任何可以轉(zhuǎn)換為字符串的數(shù)據(jù)都可以作為參數(shù))
        //                  * URL地址存在長(zhǎng)度限制,所以參數(shù)容量有限,一般用于攜帶簡(jiǎn)單的關(guān)鍵參數(shù)
        //                  * JS中有些數(shù)據(jù)類型強(qiáng)制轉(zhuǎn)換為字符串,會(huì)輸出無效數(shù)據(jù)
 <input type="button" value="URL參數(shù)-地址拼接" onclick="sendURLParams()">
  function sendURLParams(){
            axios.get("http://127.0.0.1:3000/public/params?username=admin&tel=12345678901")
                .then(( {data} )=>{
                    console.log(data);
                })
                .catch((error)=>{
                    console.log(error);
                    Notice.error({ title:"請(qǐng)求失敗" })
                })
        }

2、通過配置設(shè)置參數(shù):params:{ }

         //     2. Request payload
        //          + 請(qǐng)求體參數(shù)
        //          + 該參數(shù)的攜帶方式只能被應(yīng)用于有請(qǐng)求體的請(qǐng)求中
<input type="button" value="URL參數(shù)-通過配置設(shè)定參數(shù)" onclick="sendURLParams2()">
        function sendURLParams2(){
            axios.get("http://127.0.0.1:3000/public/params",{
                // `params` 是與請(qǐng)求一起發(fā)送的 URL 參數(shù),自動(dòng)完成參數(shù)格式狀態(tài)
                params:{
                    username:"tom",
                    tel:12345678901
                }
            })
            .then(( {data} )=>{
                console.log(data);
            })
            .catch((error)=>{
                console.log(error);
                Notice.error({ title:"請(qǐng)求失敗" })
            })
        }

3、Form Data(請(qǐng)求體參數(shù))==>動(dòng)態(tài)參數(shù),也就是說通過獲取dom節(jié)點(diǎn)的value值來動(dòng)態(tài)獲取參數(shù)

  //     3. Form Data(請(qǐng)求體參數(shù))
        //          + 請(qǐng)求體參數(shù)
        //          + 該參數(shù)的攜帶方式只能被應(yīng)用于有請(qǐng)求體的請(qǐng)求中
        // HTTP 的請(qǐng)求組成(請(qǐng)求報(bào)文)(抓包)
        //      請(qǐng)求報(bào)文頭|請(qǐng)求報(bào)文基礎(chǔ)信息 = 存儲(chǔ)請(qǐng)求必要的解釋信息(請(qǐng)求類型、請(qǐng)求格式、請(qǐng)求時(shí)長(zhǎng)……)
        //      請(qǐng)求報(bào)文體|額外的請(qǐng)求參數(shù)攜帶
        // HTTP 的響應(yīng)組成(響應(yīng)報(bào)文)
        //      響應(yīng)報(bào)文頭 = 存儲(chǔ)請(qǐng)求必要的解釋信息(請(qǐng)求類型、請(qǐng)求格式、請(qǐng)求時(shí)長(zhǎng)……)
        //      響應(yīng)報(bào)文體|服務(wù)器響應(yīng)結(jié)果
    <input id="username" type="text" value="admin">
    <input id="tel" type="text" value="12345678901">
    <input type="button" value="URL參數(shù)-動(dòng)態(tài)參數(shù)" onclick="sendURLParams3()">
       function sendURLParams3(){
            axios.get("http://127.0.0.1:3000/public/params",{
                params:{
                    username:document.getElementById("username").value,
                    tel:document.getElementById("tel").value
                }
            })
            .then(( {data} )=>{
                console.log(data);
            })
            .catch((error)=>{
                console.log(error);
                Notice.error({ title:"請(qǐng)求失敗" })
            })
        }

Axios傳參的三種方式

一:get請(qǐng)求

axios中常見的get/delete請(qǐng)求,也稱作query請(qǐng)求:

//一般發(fā)送請(qǐng)求是這么寫(不推薦):
axios.get('/user?id=12345&name=user')
.then(function (res) {
    console.log(res);
}).catch(function (err) {
    console.log(err);
});
//但是為了方便全局統(tǒng)一調(diào)用封裝的axios,我一般采用(推薦)
axios.get('/user', {  //params參數(shù)必寫 , 如果沒有參數(shù)傳{}也可以
    params: {  
       id: 12345,
       name: user
    }
})
.then(function (res) {
    console.log(res);
})
.catch(function (err) {
    console.log(err);
});

 二:post/put/patch請(qǐng)求

傳參方式大致用的有3種
(1) ‘Content-Type’= ‘multipart/form-data’
傳參格式為 formData
(全局請(qǐng)求頭:‘Content-Type’= ‘application/x-www-form-urlencoded’)
(request的Header:‘Content-Type’= ‘multipart/form-data’)

var formData=new FormData();
formData.append('user',123456);
formData.append('pass',12345678);
axios.post("/notice",formData)
     .then((res) => {return res})
     .catch((err) => {return err})

(2) ‘Content-Type’= ‘application/x-www-form-urlencoded’
傳參格式為 query 形式,使用$qs.stringify
(全局請(qǐng)求頭:‘Content-Type’= ‘application/x-www-form-urlencoded’)
(request的Header:‘Content-Type’= ‘application/x-www-form-urlencoded’)

import axios from 'axios'
import qs from 'Qs'
let data = {"code":"1234","name":"yyyy"};
axios.post(`${this.$url}/test/testRequest`,qs.stringify({
    data
}))
.then(res=>{
    console.log('res=>',res);            
})

(3) ‘Content-Type’= 'application/json
傳參格式為 raw (JSON格式)
(全局請(qǐng)求頭:‘Content-Type’= ‘application/x-www-form-urlencoded’)
(request的Header:‘Content-Type’= ‘application/json;charset=UTF-8’)

//方法一:
import axios from 'axios'
let data = {"code":"1234","name":"yyyy"};
axios.post(`${this.$url}/test/testRequest`,data)
.then(res=>{
    console.log('res=>',res);            
})
//方法二:
var readyData=JSON.stringify({
    id:1234,
    name:user
});
axios.post("/notice",readyData)
     .then((res) => {return res})
     .catch((err) => {return err})

到此這篇關(guān)于axios請(qǐng)求參數(shù)的三種方式的文章就介紹到這了,更多相關(guān)axios請(qǐng)求參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論