【发布时间】:2019-10-03 16:13:01
【问题描述】:
当我向 php 发送 post 请求时 - 它返回字符串给我,我不能用它做任何事情。
这是我对 JS 的要求:
axios.post("ajax.php", JSON.stringify(myObj))
这就是我在 PHP 中获取数据(来自 JS)的方式:
$data = $_POST;
这就是 $data var_dump 的响应
array(1) {
["{"username":"rew","info":"rew"}"]=>
string(0) ""
}
我需要 2 个变量。第一个用户名和第二个信息。我该怎么做?这条线可以拆分吗?还是我发送格式错误?
我的完整 PHP 代码
$data = array(
"userName" => $_POST['userName'],
"pass" => $_POST['pass']
);
$opts = array(
'http'=>array(
'method' => 'POST',
'content' => json_encode($data),
'header' => "Content-Type: application/json\r\n" .
"Accept: application/json\r\n" .
"Authorization: Basic " . base64_encode("$username:$password"),
)
);
$context = stream_context_create($opts);
$file = file_get_contents($remote_url, false, $context);
echo $file;
还有 var myObj
let myObj = {
"username": "rew",
"info": "rew"
};
【问题讨论】:
-
使用 json_decode 函数。更多信息在这里php.net/manual/en/function.json-decode.php
标签: javascript php ajax post