【问题标题】:send axios post request in vuejs在 vuejs 中发送 axios 发布请求
【发布时间】:2024-08-25 22:55:02
【问题描述】:

我想在 vuejs 中从我的模板发送 axios 发布请求,如下代码:

<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>

我使用 Requst 方法在 php 代码中得到 this.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>

【问题讨论】:

  • catch() 时是否有错误?
  • @Evan 没有错误:/ ?
  • 当您发布请求时页面会刷新吗?如果是的话,你能告诉我们模板代码吗
  • @Evan 我想,问题出在标题上吗?不是吗?
  • @Evan 不刷新。

标签: php vue.js axios


【解决方案1】:

更新。

你会得到Array ( [{"name":"asd"}] =&gt; ),但你应该得到这个:

Array ( ["name" => "asd"])

更新。 2

您可以尝试从 axios 的标头中删除 'Content-type': 'application/x-www-form-urlencoded', 吗?

【讨论】: