简体   繁体   English

Axios POST 请求异常数据格式

[英]Axios POST Request Unexpected Data Format

I'm busy testing the Axios plugin for Node.JS and I'm having some difficulty with POST Request.我正忙于测试 Node.JS 的 Axios 插件,但我在使用 POST 请求时遇到了一些困难。

For testing I have a Basic PHP script为了测试,我有一个基本的 PHP 脚本

// Identify GET Request
if(!empty($_GET)) {
   $type = "Req Type : GET";
   $params = $_GET;
   $array = $_GET["array"];
   $arrayVerify = gettype($array);
}

// Identify POST Request
if(!empty($_POST)) {
   $type = "Req Type : POST";
   $params = $_POST;
   $array = $_POST["array"];
   $arrayVerify = gettype($array);
}

$response = new stdClass();
$response->type = $type;
$response->array = $array;
$response->arrayVerify = $arrayVerify;
echo json_encode($response);
exit();

As an initial test I am using JQuery Ajax as follows作为初始测试,我使用 JQuery Ajax 如下

data = {};
data.array = ["One", "Two", "Three"];
$.ajax ({
   url      : "url_goes_here",
   type     : "POST",
   dataType : "json",
   data     : data,
   success  : function(res) { console.log(JSON.stringify(res)); },
   error    : function(res) { abandonAllHope(); }
});

I get the following output我得到以下 output

{"type":"Req Type : POST","array":["One","Two","Three"],"arrayVerify":"array"}

Which looks like well formed JSON, the array is still an array and PHP identified it as an array which is good看起来像格式良好的 JSON,该阵列仍然是一个阵列,PHP 将其识别为一个很好的阵列

Then when I try with Axios from Node.js然后当我尝试使用 Node.js 中的 Axios

var axios = require("axios");
var data = new URLSearchParams();
data.append("array", ["One", "Two", "Three"]);
axios ({
   url     : "url_goes_here",
   method  : "POST",
   data    : data
})
.then(function(res) { console.log(JSON.stringify(res.data)); })
.catch(function(res) { abandonAllHope(); });

I get the following output我得到以下 output

{"type":"Req Type : POST","array":"One,Two,Three","arrayVerify":"string"}

The array seems to just be a concatenation of the values and PHP identified it as a string该数组似乎只是值的串联,PHP 将其识别为字符串

The JQuery seemed to do what I expected but the Axios didn't, why is that? JQuery 似乎达到了我的预期,但 Axios 没有,这是为什么呢? How do I tell Axios to use the data as JSON?如何告诉 Axios 将数据用作 JSON?

UPDATE更新

I did also try as follows我也尝试如下

var axios = require("axios");
var data = {};
data.array = ["One", "Two", "Three"];
axios ({
   url     : lv_url,
   method  : "POST",
   data    : JSON.stringify(data)
})
.then(function(res) { console.log(JSON.stringify(res.data)); })
.catch(function(res) { });
return;

Which gave me NULLs这给了我NULL

{"type":"Req Type : POST","array":null,"arrayVerify":"NULL"}

And I also tried as我也尝试过

var axios = require("axios");
var data = {};
data.array = ["One", "Two", "Three"];
axios ({
   url     : lv_url,
   method  : "POST",
   params  : data
})
.then(function(res) { console.log(JSON.stringify(res.data)); })
.catch(function(res) { });
return;

Which gave me everything right except it's a GET request now?除了现在是 GET 请求之外,这给了我一切权利?

{"type":"Req Type : GET","array":["One","Two","Three"],"arrayVerify":"array"}

Use the option you already tried:使用您已经尝试过的选项:

var axios = require("axios");
var data = {};
data.array = ["One", "Two", "Three"];
axios ({
   url     : lv_url,
   method  : "POST",
   params  : data
})
.then(function(res) { console.log(JSON.stringify(res.data)); })
.catch(function(res) { });
return;

But in PHP side, you will not have a $_POST populated, instead use the php://input :但是在 PHP 端,您不会填充$_POST ,而是使用php://input

 $data = json_decode(file_get_contents("php://input"));

More on this answer: https://stackoverflow.com/a/8893792/1580044有关此答案的更多信息: https://stackoverflow.com/a/8893792/1580044

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

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