美文网首页前端前端学习基地
Axios传参的三种方式

Axios传参的三种方式

作者: 青争小台 | 来源:发表于2020-03-02 22:14 被阅读0次

开发过程中,经常需要全局设置,许多前端开发时容易忽略请求头的配置,常用的请求头有2种:

axios.defaults.timeout = 15000;  //超时响应
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'; // 配置请求头(推荐)
// axios.defaults.headers.post['Content-Type'] = 'application/json;charset=utf-8'; // 配置请求头
axios.defaults.baseURL = $core.use('http'); //确认协议和地址
axios.defaults.withCredentials = true;   // axios 默认不发送cookie,需要全局设置true发送cookie

一:get请求

axios中常见的get/delete请求,也称作query请求:

//一般发送请求是这么写(不推荐):
axios.get('/user?id=12345&name=user')
.then(function (res) {
    console.log(res);
}).catch(function (err) {
    console.log(err);
});

//但是为了方便全局统一调用封装的axios,我一般采用(推荐)
axios.get('/user', {  //params参数必写 , 如果没有参数传{}也可以
    params: {  
       id: 12345,
       name: user
    }
})
.then(function (res) {
    console.log(res);
})
.catch(function (err) {
    console.log(err);
});

二:post/put/patch请求

传参方式大致用的有3种
(1) 'Content-Type'= 'multipart/form-data'
传参格式为 formData

(全局请求头:'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})
image.png
(2) 'Content-Type'= 'application/x-www-form-urlencoded'
传参格式为 query 形式,使用$qs.stringify

(全局请求头:'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);            
})
image.png
(3) 'Content-Type'= 'application/json
传参格式为 raw (JSON格式)

(全局请求头:'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})
image.png

相关文章

  • Vue脚手架下axios POST传参序列化方法

    Vue工程化项目下axios POST传参序列化方法 axios默认POST传参方式为Query String P...

  • Axios传参的三种方式

    开发过程中,经常需要全局设置,许多前端开发时容易忽略请求头的配置,常用的请求头有2种: 一:get请求 axios...

  • Vue post请求的坑

    本文使用的是vue的Axios的请求方式 axios的get请求传参方法正常,但是post得请求方法传参一直报40...

  • 2018-09-26传参 和axion

    传参 axios 下载:npm install axios版本1.0:vue-resource2.0:axios(...

  • 路由

    传参 axios 下载:npm install axios版本1.0:vue-resource2.0:axios(...

  • vector 作为函数参数

    参考 C++(笔记)容器(vector)作为函数参数如何传参 vector作为参数的三种传参方式

  • python函数详解

    函数定义 格式 函数定义实例: 5050 三种传参方式 按位置顺序传参 李诗才 按关键字传参,参数顺序可以任意 李...

  • axios-get方式传参

    params里面传的参数就是url里面?后面的&userId=''&page=''&pageSize='' 值或者...

  • 异步请求那点事儿

    fetch get 请求 默认请求方式,不能传参 post 请求 参数为字符串类型 axios get请求 pos...

  • Vue 封装axios,返回指定的response

    1. 需求 定制请求,将get,post方式进行封装axios,传参都以对象方式传递 2. ajax.js 请求使用

网友评论

    本文标题:Axios传参的三种方式

    本文链接:https://www.haomeiwen.com/subject/tjwikhtx.html