简体   繁体   English

为什么axios总是发送空对象?

[英]Why does axios always send empty object?

I'm trying to figure this out and it's driving me mad. 我试图弄清楚这一点,这使我发疯。 I am trying to send an object of data using an axios post request. 我正在尝试使用axios发布请求发送数据对象。 It goes to the file okay but the object is always empty. 可以进入文件,但是对象始终为空。 So when I use this code: 因此,当我使用此代码时:

axios.post('php/send_email.php', {
  params: {
    name: 'niall'
  }
})
.then(function (result) {

  console.log(result)

});

And then use the php below: 然后使用下面的PHP:

<?php
  echo $_POST['name'];
?>

It will always output an error of name being undefined for the result from the http request.Can anyone shed some light on this and where I am going wrong? 它总是会输出一个错误名称,该错误名称对于http请求的结果是未定义的。任何人都可以阐明这个错误以及我要去哪里了吗?

Also I noticed that this seems to be a problem with sending an object because when I try: 我也注意到发送对象似乎是一个问题,因为当我尝试时:

axios.post('php/send_email.php', 'niall' )
.then(function (result) {
  console.log(result)
});

And then print out the array using: 然后使用以下命令打印出数组:

<?php
 print_r($_POST);
?>

It will show: 它会显示:

Object {data: "Array↵(↵ [niall] => ↵)↵", status: 200, statusText: "OK", headers: Object, config: Object…} 对象{数据:“Array↵(↵[niall] =>↵)↵”,状态:200,statusText:“确定”,标题:Object,配置:Object…}

Try sending like this 尝试像这样发送

axios.post('php/send_email.php', { name: 'niall' }})

instead of wrapping parameters in an extra params object. 而不是将参数包装在额外的params对象中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM