vue如何把data的值,全部一次性post到后台中去,并获取呢?

1、前台data的值如下:

var vm = new Vue({
  el: '#vm',
    data: {
        offer: {
            isshow:false,
            sum_total_price:0,
            sum_discount:0,
            sum_price:0,
            children:[
            {
              type_id:'1',
              type_name:'机票',
              title: '',
              date: '',
              price: 0,
              number:1,
              time:1,
              total_price:0,
              discount: 0,
              other: '',
            }, {
              type_id:'2',
              type_name:'酒店',
              title: '',
              date: '',
              price: 0,
              number:1,
              time:1,
              total_price:0,
              discount: 0,
              other: '',
            }, {
              type_id:'3',
              type_name:'签证',
              title: '',
              date: '',
              price: 0,
              number:1,
              time:1,
              total_price:0,
              discount: 0,
              other: '',
            }, {
              type_id:'4',
              type_name:'保险',
              title: '',
              date: '',
              price: 0,
              number:1,
              time:1,
              total_price:0,
              discount: 0,
              other: '',
            }, {
              type_id:'5',
              type_name:'商务考察',
              title: '',
              date: '',
              price: 0,
              number:1,
              time:1,
              total_price:0,
              discount: 0,
              other: '',
            }, {
              type_id:'6',
              type_name:'用车',
              title: '',
              date: '',
              price: 0,
              number:1,
              time:1,
              total_price:0,
              discount: 0,
              other: '',
            }, {
              type_id:'7',
              type_name:'餐饮',
              title: '',
              date: '',
              price: 0,
              number:1,
              time:1,
              total_price:0,
              discount: 0,
              other: '',
            }, {
              type_id:'8',
              type_name:'导游',
              title: '',
              date: '',
              price: 0,
              number:1,
              time:1,
              total_price:0,
              discount: 0,
              other: '',
            }, {
              type_id:'9',
              type_name:'轮船',
              title: '',
              date: '',
              price: 0,
              number:1,
              time:1,
              total_price:0,
              discount: 0,
              other: '',
            }, {
              type_id:'10',
              type_name:'火车',
              title: '',
              date: '',
              price: 0,
              number:1,
              time:1,
              total_price:0,
              discount: 0,
              other: '',
            }, {
              type_id:'11',
              type_name:'门票',
              title: '',
              date: '',
              price: 0,
              number:1,
              time:1,
              total_price:0,
              discount: 0,
              other: '',
            }, {
              type_id:'12',
              type_name:'会议',
              title: '',
              date: '',
              price: 0,
              number:1,
              time:1,
              total_price:0,
              discount: 0,
              other: '',
            }, {
              type_id:'13',
              type_name:'服务费',
              title: '',
              date: '',
              price: 0,
              number:1,
              time:1,
              total_price:0,
              discount: 0,
              other: '',
            }, {
              type_id:'14',
              type_name:'团建拓展',
              title: '',
              date: '',
              price: 0,
              number:1,
              time:1,
              total_price:0,
              discount: 0,
              other: '',
            }, {
              type_id:'15',
              type_name:'其他',
              title: '',
              date: '',
              price: 0,
              number:1,
              time:1,
              total_price:0,
              discount: 0,
              other: '',
            }
            ]
        },
        title:'方案',
        date:'2018-12-12',
        adult:'0',
        children:'0',
        gocity:'宁波',
        tocity:'上海',
        special:'',
        tour:[
                {title:'',isshow:false,children:[
                    {title:"机票",type:1,other:"",children:[
                        {flight_number:'',flight_space:'1',start_time:'',start_airport:'',start_terminal:'',end_time:'',end_airport:'',end_terminal:'',stop_city:'',stop_hour:'',stop_minute:'',overnight:''},
                        {flight_number:'',flight_space:'1',start_time:'',start_airport:'',start_terminal:'',end_time:'',end_airport:'',end_terminal:'',stop_city:'',stop_hour:'',stop_minute:'',overnight:''}
                    ]},
                    {title:"景点",type:2,other:"",children:[
                        {sight_name:"",sight_id:"",img:""},
                        {sight_name:"",sight_id:"",img:""}
                    ]},
                    {title:"酒店",type:3,other:"",children:[
                        {hotel_name:'',hotel_id:'',img:''},
                        {hotel_name:'',hotel_id:'',img:''}
                    ]},
                    {title:"餐饮",type:4,meal:1,other:"",children:[
                        {cater_name:'',cater_id:'',img:''},
                        {cater_name:'',cater_id:'',img:''}
                    ]},
                    {title:'交通',type:5,other:''},
                    {title:'活动',type:6,other:''}
                ]}
            ]
    }
})

2、页面也引入了axios文件:

<script src="https://unpkg.com/axios/dist/axios.min.js"></script>

3、测试提交代码如下:

        axios.post("{:url('api/plan/save')}", {
             firstName: 'Fred',
             lastName: 'Flintstone'
        })
        .then(function (response) {
            console.log(response);
        })
        .catch(function (error) {
            console.log(error);
        });

4、数据是post过去了,状态2000,但是后台php $_POST 获取不到post过来的数据,需要怎么操作?
另外如果把data里的值全部post过去,又是怎么操作的?

补充:
又加了个Qs文件

JS文件引入
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="https://cdn.bootcss.com/qs/6.5.2/qs.min.js"></script>

//post请求代码
    let postData={
        title:vm.title,
        date:vm.date,
        adult:vm.adult,
        children:vm.children,
        gocity:vm.gocity,
        tocity:vm.tocity
    }

    axios.post("{:url('api/plan/save')}",Qs.stringify(postData))
    .then(function (response) {
        console.log(response);
    })
    .catch(function (error) {
        console.log(error);
    });

//php后台可以用$_POST接收到的数据了,数据如下,但中文出现了乱码?
array (size=6)
  'title' => string '鏂规' (length=6)
  'date' => string '2018-12-12' (length=10)
  'adult' => string '0' (length=1)
  'children' => string '0' (length=1)
  'gocity' => string '瀹佹尝' (length=6)
  'tocity' => string '涓婃捣' (length=6)

//换个浏览器查看又是正常的,看来是chrome的问题了
array (size=6)
  'title' => string '方案' (length=6)
  'date' => string '2018-12-12' (length=10)
  'adult' => string '0' (length=1)
  'children' => string '0' (length=1)
  'gocity' => string '宁波' (length=6)
  'tocity' => string '上海' (length=6)

另外,怎么把vue的整个data值像postData一样给post过去?
目前只是自己先对应data的值,拼接到postData,再post过去的?

阅读 3.9k
3 个回答

$_POST 只能接收 Content-Type: application/x-www-form-urlencoded 提交的数据

接口 post 传参使用 file_get_contents("php://input") 来获取

设置content-type:'application/x-www-form-urlencoded'
用data:{}传值

新手上路,请多包涵

vm.$data

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题