![](/img/trans.png)
[英]POST large array from Javascript to PHP with XMLHttpRequest
[英]How to display POST values which are coming from javascript XMLHttpRequest() in php
我正在使用XMLHttpRequest()javascript函數將參數發送到Json formate中的另一個php頁面,但是$ _POST ['appoverGUID']無法獲取發布值。
這是我的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_decode
。 像這樣:
if ("application/json" === getallheaders())
$_JSON = json_decode(file_get_contents("php://input"), true) ?: [];
以此方式填充參數(此處未對ApproverGUID內容進行轉義/編碼):
params = "appoverGUID="+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;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.