<tfoot id='520kA'></tfoot>
<i id='520kA'><tr id='520kA'><dt id='520kA'><q id='520kA'><span id='520kA'><b id='520kA'><form id='520kA'><ins id='520kA'></ins><ul id='520kA'></ul><sub id='520kA'></sub></form><legend id='520kA'></legend><bdo id='520kA'><pre id='520kA'><center id='520kA'></center></pre></bdo></b><th id='520kA'></th></span></q></dt></tr></i><div id='520kA'><tfoot id='520kA'></tfoot><dl id='520kA'><fieldset id='520kA'></fieldset></dl></div>
    <bdo id='520kA'></bdo><ul id='520kA'></ul>

<small id='520kA'></small><noframes id='520kA'>

      1. <legend id='520kA'><style id='520kA'><dir id='520kA'><q id='520kA'></q></dir></style></legend>

        axios http 总是返回空数据

        时间:2023-07-15

      2. <tfoot id='FXPvb'></tfoot>
          <tbody id='FXPvb'></tbody>

        <small id='FXPvb'></small><noframes id='FXPvb'>

        • <bdo id='FXPvb'></bdo><ul id='FXPvb'></ul>

              1. <i id='FXPvb'><tr id='FXPvb'><dt id='FXPvb'><q id='FXPvb'><span id='FXPvb'><b id='FXPvb'><form id='FXPvb'><ins id='FXPvb'></ins><ul id='FXPvb'></ul><sub id='FXPvb'></sub></form><legend id='FXPvb'></legend><bdo id='FXPvb'><pre id='FXPvb'><center id='FXPvb'></center></pre></bdo></b><th id='FXPvb'></th></span></q></dt></tr></i><div id='FXPvb'><tfoot id='FXPvb'></tfoot><dl id='FXPvb'><fieldset id='FXPvb'></fieldset></dl></div>
              2. <legend id='FXPvb'><style id='FXPvb'><dir id='FXPvb'><q id='FXPvb'></q></dir></style></legend>
                  本文介绍了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 总是返回空数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:axios POST上的PHP Post数组为空 下一篇:react.js 使用 axios 将数据发布到 php,但 php echo 为空

                  相关文章

                    • <bdo id='Tk3ss'></bdo><ul id='Tk3ss'></ul>

                    <legend id='Tk3ss'><style id='Tk3ss'><dir id='Tk3ss'><q id='Tk3ss'></q></dir></style></legend>

                    1. <i id='Tk3ss'><tr id='Tk3ss'><dt id='Tk3ss'><q id='Tk3ss'><span id='Tk3ss'><b id='Tk3ss'><form id='Tk3ss'><ins id='Tk3ss'></ins><ul id='Tk3ss'></ul><sub id='Tk3ss'></sub></form><legend id='Tk3ss'></legend><bdo id='Tk3ss'><pre id='Tk3ss'><center id='Tk3ss'></center></pre></bdo></b><th id='Tk3ss'></th></span></q></dt></tr></i><div id='Tk3ss'><tfoot id='Tk3ss'></tfoot><dl id='Tk3ss'><fieldset id='Tk3ss'></fieldset></dl></div>
                      <tfoot id='Tk3ss'></tfoot>

                      <small id='Tk3ss'></small><noframes id='Tk3ss'>