axios http 总是返回空数据 [英] axios http always returns with empty data

查看:76
本文介绍了axios http 总是返回空数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前问过这个问题,但无法得到答案.我能够像下面那样使用方法:'get'来让它工作所以没关系但是这次我需要使用post.在另一个项目中(使用 react、redux、php、webpack、xampp),同样的问题又出现了,我正在努力解决这个问题.所以这里是:

register.php

 echo $_GET['task'];

index.js

 const values = {task: 'doSomething', username: 'username'}轴({url: "./server/register.php",超时:20000,方法:'获取',参数:值}).then(function(response){console.log(response.data)})

当我执行上述操作时,一切正常,数据以doSomething"的形式注销.但是,当我尝试使用 axios({method: 'POST'}) 并将 php 更改为 $_POST['task'] 时,我收到一条错误消息,指出 $_POST['task'] 未定义,如下所示:

index.js

 axios({url: "/projects/myProject/server/register.php",方法:'发布',数据:值}).then(function(response){console.log(response.data)})

register.php

echo $_POST['task'];

<块引用>

注意:未定义索引:任务

此外,当我使用 axios.post() 尝试此操作时,我遇到了完全相同的问题.我想在这里使用 post 请求.任何人都可以为我解释一下吗?

解决方案

好吧,经过一番摸索,我找到了答案.在 PHP 上,必须先添加这一行,然后才能访问任何 POST 数据:

$_POST = json_decode(file_get_contents('php://input'), true);echo $_POST['task'];

根据我的理解,从 axios 输入的数据是 JSON,因此我们必须使用 file_get_contents() 以 JSON 编码的字符串形式返回它,然后使用 json_decode 将其从 JSON 编码的字符串转换为 php 变量.希望这对其他人有帮助.谢谢你.

I asked this question before and wasn't able to get an answer. I was able to do use method: 'get' like below to get it working so it was okay but this time I need to use post. In a different project (using react, redux, php, webpack, xampp) the same issue has resurfaced and I am trying to figure it out. So here it is:

register.php

  echo $_GET['task'];

index.js

  const values = {task: 'doSomething', username: 'username'}
  axios({
      url: "./server/register.php",
      timeout: 20000,
      method: 'get',
      params: values
  }).then(function(response){console.log(response.data)})

When I do the above everything is okay and the data is logged out as 'doSomething'. However, when I try using axios({method: 'POST'}) and changing the php to $_POST['task'] I get an error saying that $_POST['task'] is undefined like below:

index.js

  axios({
      url: "/projects/myProject/server/register.php",
      method: 'post',
      data: values
  }).then(function(response){console.log(response.data)})

register.php

echo $_POST['task'];

Notice: Undefined index: task

Also when I try this using axios.post() I encounter the exact same problem. I want to use a post request here. Can anyone shed some light on this for me?

解决方案

Okay after a fair amount of scratching my head I have found an answer. On the PHP this line has to be added before I can access any POST data:

$_POST = json_decode(file_get_contents('php://input'), true);
echo $_POST['task'];

From my understanding the data being inputted from axios is JSON so we must return it in a JSON encoded string using file_get_contents() and then convert this into a php variable from the JSON encoded string using json_decode. Hope this helps someone else. Thank you.

这篇关于axios http 总是返回空数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆