axios http 总是返回空数据

2023-07-15php开发问题
1

本文介绍了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 总是返回空数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

PHP实现DeepL翻译API调用
DeepL的翻译效果还是很强大的,如果我们要用php实现DeepL翻译调用,该怎么办呢?以下是代码示例,希望能够帮到需要的朋友。 在这里需要注意,这个DeepL的账户和api申请比较难,不支持中国大陆申请,需要拥有香港或者海外信用卡才行,没账号的话,目前某宝可以...
2025-08-20 php开发问题
168

PHP通过phpspreadsheet导入Excel日期数据处理方法
PHP通过phpspreadsheet导入Excel日期,导入系统后,全部变为了4开头的几位数字,这是为什么呢?原因很简单,将Excel的时间设置问文本,我们就能看到该日期本来的数值,上图对应的数值为: 要怎么解决呢?进行数据转换就行,这里可以封装方法,或者用第三方的...
2024-10-23 php开发问题
287

mediatemple - 无法使用 codeigniter 发送电子邮件
mediatemple - can#39;t send email using codeigniter(mediatemple - 无法使用 codeigniter 发送电子邮件)...
2024-08-23 php开发问题
11

Laravel Gmail 配置错误
Laravel Gmail Configuration Error(Laravel Gmail 配置错误)...
2024-08-23 php开发问题
16

将 PHPMailer 用于 SMTP 的问题
Problem with using PHPMailer for SMTP(将 PHPMailer 用于 SMTP 的问题)...
2024-08-23 php开发问题
4

关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题
Issue on how to setup SMTP using PHPMailer in GoDaddy server(关于如何在 GoDaddy 服务器中使用 PHPMailer 设置 SMTP 的问题)...
2024-08-23 php开发问题
17