vue
因此交互使用的是官方推薦的axios
,在jquery
中使用ajax
時收發數據都沒有問題,可是使用axios
的post
方法時發現一些問題,根據本身的理解記錄一下。methods:{
axios_post:function(){
axios
.post('https://www.xxxxxx.cn/xx/xx.php',{
UserName:'xipengheng',
msg:'我是一頭長頸鹿,我愛上樹',
msgTime:'2019.10.23'
})
.then(res=>{
console.log(res);
})
}
},
複製代碼
$UserName='"'.$_POST['UserName'].'"';
$msg='"'.$_POST['msg'].'"';
$msgTime='"'.$_POST['msgTime'].'"';
複製代碼
PHP
中個人SQL
語句是沒有數據的,所以也無法往數據庫中存儲數據
PHP
代碼的狀況下有什麼解決辦法呢?一、經過實例化一個FormData
把數據放入就能夠了(推薦)。javascript
methods: {
axios_post:()=> {
var params = new FormData();
params.append('UserName', 'xipengheng');
params.append('msg', '我是一頭長頸鹿,我愛上樹');
params.append('msgTime', '2009.09.09');
axios
.post('https://www.xipengheng.cn/AAA/liuyan.php',params)
.then(res => {
console.log(res);
})
}
},
複製代碼
qs
,利用數據轉化爲qs.stringtry({})
,也能夠實現methods:{
axios_post:()=>{
axios
.post('https://www.xipengheng.cn/AAA/liuyan.php',qs.stringify({
UserName:'xipengheng',
msg:'我是一頭長頸鹿,我愛上樹',
msgTime:'2009.99.09'
}))
.then(res=>{
console.log(res);
})
}
},
複製代碼