[英]How to display POST values which are coming from javascript XMLHttpRequest() in php
I am sending parameters using XMLHttpRequest() javascript function to another php page in Json formate, but $_POST['appoverGUID'] not getting post values. 我正在使用XMLHttpRequest()javascript函数将参数发送到Json formate中的另一个php页面,但是$ _POST ['appoverGUID']无法获取发布值。
Here is my Javascript code. 这是我的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);
}
And here is my json.php file code. 这是我的json.php文件代码。
if(isset($_POST['appoverGUID'])){
echo $_POST['appoverGUID'];
}
You need use json_decode
. 您需要使用json_decode
。 Some like this: 像这样:
if ("application/json" === getallheaders())
$_JSON = json_decode(file_get_contents("php://input"), true) ?: [];
Fill params this way (did no escaping/encoding of approverGUID content, here..): 以此方式填充参数(此处未对ApproverGUID内容进行转义/编码):
params = "appoverGUID="+approverGUID;
Also see: 另请参阅:
http://www.openjs.com/articles/ajax_xmlhttp_using_post.php http://www.openjs.com/articles/ajax_xmlhttp_using_post.php
First of all remove these headers since they will be send automatically by the browser and it's the right way to do it. 首先删除这些标头,因为它们将由浏览器自动发送,这是正确的方法。
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");
This code is a cross browser solution and it's tested. 此代码是跨浏览器解决方案,已经过测试。
// 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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.