Php 在vuejs中发送axios post请求
我想从vuejs中的模板发送axios post请求,如下代码所示:Php 在vuejs中发送axios post请求,php,vue.js,axios,Php,Vue.js,Axios,我想从vuejs中的模板发送axios post请求,如下代码所示: <script> export default { data() { return { body:{ name:'ssdsdsd', time:'232342', }, header:{headers: { 'Content-Type': 'application/x-www-form-urlencoded', }},
<script>
export default {
data() {
return {
body:{
name:'ssdsdsd',
time:'232342',
},
header:{headers: {
'Content-Type': 'application/x-www-form-urlencoded',
}},
name:null,
r:null,
}
},
methods: {
post:function(){
this.$http.post('http://127.0.0.1/saver.php',this.body,this.header).then(r => {
this.r=r;
// eslint-disable-next-line no-console
console.log(typeof this.body)
}).catch(err => {
// eslint-disable-next-line no-console
console.log(err.response.data)
});
}
},
}
</script>
导出默认值{
数据(){
返回{
正文:{
名称:'SSDSD',
时间:'232342',
},
标题:{标题:{
“内容类型”:“应用程序/x-www-form-urlencoded”,
}},
名称:空,
r:空,
}
},
方法:{
职位:职能(){
这是.$http.post('http://127.0.0.1/saver.php,this.body,this.header)。然后(r=>{
这个。r=r;
//eslint禁用下一行无控制台
console.log(此.body的类型)
}).catch(错误=>{
//eslint禁用下一行无控制台
console.log(err.response.data)
});
}
},
}
我用Requst方法在php代码中得到了这个.name(name)
这是我的php代码:
<?php
header('Access-Control-Allow-Origin: *');
header('Content-type: application/json');
echo($_REQUEST['name']);
?>
但php返回了以下响应:
<br />
<font size='1'><table class='xdebug-error xe-notice' dir='ltr' border='1' cellspacing='0' cellpadding='1'>
<tr><th align='left' bgcolor='#f57900' colspan="5"><span style='background-color: #cc0000; color: #fce94f; font-size: x-large;'>( ! )</span> Notice: Undefined index: name in C:\wamp64\www\saver.php on line <i>4</i></th></tr>
<tr><th align='left' bgcolor='#e9b96e' colspan='5'>Call Stack</th></tr>
<tr><th align='center' bgcolor='#eeeeec'>#</th><th align='left' bgcolor='#eeeeec'>Time</th><th align='left' bgcolor='#eeeeec'>Memory</th><th align='left' bgcolor='#eeeeec'>Function</th><th align='left' bgcolor='#eeeeec'>Location</th></tr>
<tr><td bgcolor='#eeeeec' align='center'>1</td><td bgcolor='#eeeeec' align='center'>0.0003</td><td bgcolor='#eeeeec' align='right'>401952</td><td bgcolor='#eeeeec'>{main}( )</td><td title='C:\wamp64\www\saver.php' bgcolor='#eeeeec'>...\saver.php<b>:</b>0</td></tr>
</table></font>
(!)注意:第4行C:\wamp64\www\saver.php中的未定义索引:name
调用堆栈
#时间记忆功能定位
10.0003401952{main}()…\saver.php:0
Upd.
您将获得数组([{“name”:“asd”}]=>)
但您应该得到以下内容:
Array ( ["name" => "asd"])
Upd。2
您能否尝试从axios中的标题中删除
'Content-type':'application/x-www-form-urlencoded',
?执行catch()时是否有错误?@Evan无错误:/?发布请求时页面是否刷新?如果有,你能给我们看一下模板吗code@Evan我想,问题是来自头球?不是吗?@Evan No不刷新。评论不用于扩展讨论;这段对话已经结束。