我正在使用此方法将参数发送到我的服务器 php,但我得到了您发布的发货值:
function post(path, parameters) {
var http = new XMLHttpRequest();
console.log(parameters);
http.open("POST", path, true);
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.send(parameters);
}
PHP:
public function tracking_referidos(){
$this->autoRender = false;
$result = array();
$result['post'] = $_POST;
echo json_encode($result);
exit;
}
结果:{"post":{"referrer":"","event":"eventrid","hash":"45hfkabdus"}}
最佳答案
您正在发送一个 JSON 字符串。 PHP 不会自动解码该数据并将其映射到 $_POST
super 全局。如果您希望 PHP 这样做,您需要将数据作为 application/x-www-form-urlencoded
发送(即类似于 get 请求的 URI:key=value&key2=value2
)。
您可以使用 application/json
内容类型发送数据,但要获取请求数据,您需要阅读原始帖子正文。您可以在 php://input
流中找到它。只需使用 file_get_contents
读取它:
$rawPostBody = file_get_contents('php://input');
$postData = json_decode($rawPostBody, true);//$postData is now an array
关于javascript - 带有发布数据的请求后,PHP 中的 $_POST 数组为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33961016/