Vue-axios使用QS(QueryString)插件,Vue-axios无法发送参数给后端(包含但不限于php)。

推荐方法(最简单):

步骤一:
导入QueryString:

<script src="//cdn.bootcss.com/qs/6.5.2/qs.min.js"></script>

步骤二:
Window.QS就好了

终于搞定了,有没有点儿小激动.
methods:{
            Get_Visit:function () {
                var that = this;
                axios.post('index.php',
                    window.Qs.stringify({
                    method:"visit",
                    }),
                ).then(function (response) {
                     console.log(response);
                        that.visit_time = response.data.visittime;
                        console.log(that.visit_time);
                        }).catch(function (error) {
                        console.log(error);
                             });
            }
        },

在这里插入图片描述
其他方法1(后方附代码):

在这里插入图片描述
下载
axios 自带 qs 可直接引用
npm i qs or yarn add qs
引入
// 在main.js
import axios from ‘axios’;
Vue.prototype. h t t p = a x i o s ; i m p o r t q s f r o m ′ q s ′ V u e . p r o t o t y p e . http = axios; import qs from 'qs' Vue.prototype. http=axios;importqsfromqsVue.prototype.qs = qs;
使用

//在 xx.vue
let params= {
code:“1234”,
name:“yyyy”
};
this. h t t p . p o s t ( ′ u r l ′ , t h i s . http.post('url',this. http.post(url,this.qs.stringify( params)
).then(res=>{
console.log(‘res=>’,res);
})
下载
axios 自带 qs 可直接引用
npm i qs or yarn add qs
引入
// 在main.js
import axios from ‘axios’;
Vue.prototype. h t t p = a x i o s ; i m p o r t q s f r o m ′ q s ′ V u e . p r o t o t y p e . http = axios; import qs from 'qs' Vue.prototype. http=axios;importqsfromqsVue.prototype.qs = qs;
使用

//在 xx.vue
let params= {
code:“1234”,
name:“yyyy”
};
this. h t t p . p o s t ( ′ u r l ′ , t h i s . http.post('url',this. http.post(url,this.qs.stringify( params)
).then(res=>{
console.log(‘res=>’,res);
})

作者:不遭人妒是庸才
链接:https://www.jianshu.com/p/cc9a4bcf2610
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

其他方法2:
在这里插入图片描述
https://blog.youkuaiyun.com/WebDestiny/article/details/100558371

Vue项目中,使用axios的get请求携带对象参数后端发送请求时出现400错误,通常是因为GET请求的参数格式不正确。GET请求的参数需要以查询字符串的形式附加在URL后面,而不是直接传递一个对象。 以下是一些解决方法: 1. **使用URLSearchParams**: 可以使用URLSearchParams来序列化参数。 ```javascript import axios from 'axios'; const params = new URLSearchParams(); params.append('param1', 'value1'); params.append('param2', 'value2'); axios.get('/api/endpoint', { params }) .then(response => { console.log(response.data); }) .catch(error => { console.error(error); }); ``` 2. **使用qs库**: 可以使用qs库来序列化参数。 ```javascript import axios from 'axios'; import qs from 'qs'; const data = { param1: 'value1', param2: 'value2' }; axios.get('/api/endpoint', { params: data, paramsSerializer: params => { return qs.stringify(params, { arrayFormat: 'brackets' }) } }) .then(response => { console.log(response.data); }) .catch(error => { console.error(error); }); ``` 3. **手动拼接URL**: 手动将参数拼接在URL后面。 ```javascript import axios from 'axios'; const params = { param1: 'value1', param2: 'value2' }; let queryString = Object.keys(params).map(key => key + '=' + params[key]).join('&'); let url = `/api/endpoint?${queryString}`; axios.get(url) .then(response => { console.log(response.data); }) .catch(error => { console.error(error); }); ``` 通过这些方法,可以确保GET请求的参数以正确的格式传递给后端,从而避免400错误。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值