前端用Vue给后台PHP传参,后台接收不到???怎么办

我们在使用axios时,不知道大家有没有遇到这样一个问题,明明前端把参数传过去了,但是用php写的后台却接收不到,其他的后台语言却可以接收到,具体的原理大家可以自己去百度一下,下面只给大家提供一下解决的办法。

第一步:安装一下ElementUI,命令为 

cnpm i ElementUI

第二步:安装一个qs插件,命令为

cnpm i qs

第三步:安装axios,命令为

cnpm i axios

接下来我们在自己的入口的js文件中引入并配置这三个东西

第一步:引入ElementUI,并挂载到vue身上,代码为

import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';
Vue.use(ElementUI);

第二步:引入axios,并配置自己的axios,代码为

import axios from 'axios'
// 配置请求的域名
axios.defaults.baseURL='http://192.168.16.155/tp_datateach/public/index.php/index'
Vue.prototype.$ajax=axios

第三步:引入qs,并配置自己的qs和发送给后台数据的格式,代码为

// 超时时间
axios.defaults.timeout = 5000;
// http请求拦截器
axios.interceptors.request.use(config => {
  if(config.method=='post'){
    config.data=qs.stringify(config.data);//防止post请求参数无法传到后台  在这里就用到了qs
  }
  let loading = ElementUI.Loading.service({//使用element-ui的加载条组件
    fullscreen: true,
    text: '拼命加载中...',
  });
  return config
}, error => {
  let loading = ElementUI.Loading.service({});
  loading.close();    //关闭加载前,记得重新定义实例
  return Promise.reject(error)
});
// http响应拦截器
axios.interceptors.response.use(data => {
  let loading = ElementUI.Loading.service({});
  loading.close();
  return data
}, error => {
  let loading = ElementUI.Loading.service({
    fullscreen: true,
    text: '拼命加载中...',
  });
  loading.close();
  return Promise.reject(error)
});

此时,你给后台传递的参数应该就可以成功的发送过去了。

猜你喜欢

转载自blog.csdn.net/weixin_42689147/article/details/102526435
今日推荐