如何在php中显示来自javascript XMLHttpRequest()的POST值

如何在php中显示来自javascript XMLHttpRequest()的POST值,javascript,php,jquery,ajax,json,Javascript,Php,Jquery,Ajax,Json,我正在使用XMLHttpRequest()javascript函数将参数发送到另一个Json格式的php页面,但是$\u POST['appoverGUID']没有获取POST值 这是我的Javascript代码 function loadPage(href){ var http = new XMLHttpRequest(); var url = json.php; var approverGUID = "

我正在使用XMLHttpRequest()javascript函数将参数发送到另一个Json格式的php页面,但是$\u POST['appoverGUID']没有获取POST值

这是我的Javascript代码

        function loadPage(href){

            var http = new XMLHttpRequest();
            var url = json.php;
            var approverGUID = "Test";
            var params = JSON.stringify({ appoverGUID: approverGUID });
            http.open("POST", url, true);
            http.setRequestHeader("Content-type", "application/json; charset=utf-8");
            http.setRequestHeader("Content-length", params.length);
            http.setRequestHeader("Connection", "close");
             http.onreadystatechange = function() {
                if(http.readyState == 4 && http.status == 200) {
                    document.getElementById('bottom').innerHTML = http.responseText;

                }
            } 
            http.send(params);

        }
这是我的json.php文件代码

if(isset($_POST['appoverGUID'])){
echo $_POST['appoverGUID'];
}

您需要使用
json\u decode
。有些人喜欢这样:

if ("application/json" === getallheaders())
    $_JSON = json_decode(file_get_contents("php://input"), true) ?: [];

您需要使用
json\u decode
。有些人喜欢这样:

if ("application/json" === getallheaders())
    $_JSON = json_decode(file_get_contents("php://input"), true) ?: [];
以这种方式填充参数(此处没有对approverGUID内容进行转义/编码..):

另见:

以这种方式填充参数(此处没有转义/编码approverGUID内容):

另见:


首先删除这些标题,因为它们将由浏览器自动发送,这是正确的方法

http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
这段代码是一个跨浏览器的解决方案,已经过测试

// IE 5.5+ and every other browser
var xhr = new(window.XMLHttpRequest || ActiveXObject)('MSXML2.XMLHTTP.3.0');
var params = "appoverGUID="+approverGUID;
xhr.open("POST", url, true);

xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
xhr.setRequestHeader("Accept", "application/json");
xhr.onreadystatechange = function () {
    if (this.readyState === 4) {
        if (this.status >= 200 && this.status < 400) {
            console.log(JSON.parse(this.responseText));
        }
    }
}
xhr.send(params);
xhr = null;
//IE 5.5+和其他浏览器
var xhr=new(window.XMLHttpRequest | | ActiveXObject)('MSXML2.XMLHTTP.3.0');
var params=“appoverGUID=“+approverGUID;
xhr.open(“POST”,url,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded;字符集=UTF-8”);
setRequestHeader(“接受”、“应用程序/json”);
xhr.onreadystatechange=函数(){
if(this.readyState==4){
如果(this.status>=200&&this.status<400){
log(JSON.parse(this.responseText));
}
}
}
xhr.send(参数);
xhr=null;

首先删除这些标题,因为它们将由浏览器自动发送,这是正确的方法

http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
这段代码是一个跨浏览器的解决方案,已经过测试

// IE 5.5+ and every other browser
var xhr = new(window.XMLHttpRequest || ActiveXObject)('MSXML2.XMLHTTP.3.0');
var params = "appoverGUID="+approverGUID;
xhr.open("POST", url, true);

xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
xhr.setRequestHeader("Accept", "application/json");
xhr.onreadystatechange = function () {
    if (this.readyState === 4) {
        if (this.status >= 200 && this.status < 400) {
            console.log(JSON.parse(this.responseText));
        }
    }
}
xhr.send(params);
xhr = null;
//IE 5.5+和其他浏览器
var xhr=new(window.XMLHttpRequest | | ActiveXObject)('MSXML2.XMLHTTP.3.0');
var params=“appoverGUID=“+approverGUID;
xhr.open(“POST”,url,true);
setRequestHeader(“内容类型”,“应用程序/x-www-form-urlencoded;字符集=UTF-8”);
setRequestHeader(“接受”、“应用程序/json”);
xhr.onreadystatechange=函数(){
if(this.readyState==4){
如果(this.status>=200&&this.status<400){
log(JSON.parse(this.responseText));
}
}
}
xhr.send(参数);
xhr=null;

您的JS变量名为
approverGUID
,还是输入错误?您的JS变量名为
approverGUID
,还是输入错误?