[英]$_POST array empty in PHP after an a request with post data
我正在使用此方法向我的服务器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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.