0
在舊$就法,我要送陣列和正常的數據,像這樣:的XMLHttpRequest - 發送陣列由彥博
var dataARR = {
byteValue: 1,
data: []
};
注data
是一個數組。
dataARR.data.push({Username: "wazo", Info: "wazoinfo"});
dataARR.data.push({Username: "bifo", Info: "bifoinfo"});
然後得到它準備和發送:
var dataJSON = JSON.stringify(dataARR);
$.ajax({
type: "POST", // This for array
url: "login.php",
cache: false,
data: { myJson: dataJSON },
success: function (data) {
// Do stuff here
}
});
在PHP代碼,然後我會做到以下幾點:
$dataTotal = json_decode($post['myJson'], true);
$Username0 = $dataTotal['data'][0]['Username'];
$Info0 = $dataTotal['data'][0]['Info']);
$Username1 = $dataTotal['data'][1]['Username'];
$Info1 = $dataTotal['data'][1]['Info']);
所有這一切運作良好。
不過,現在我改變到的XMLHttpRequest:
var url = "login.php";
var method = "POST";
var xhr = new XMLHttpRequest();
if ("withCredentials" in xhr) {
// XHR for Chrome/Firefox/Opera/Safari.
xhr.open(method, url, true);
} else if (typeof XDomainRequest != "undefined") {
// XDomainRequest for IE.
xhr = new XDomainRequest();
xhr.open(method, url);
} else {
// CORS not supported.
xhr = null;
}
if (!xhr) {
return;
}
// Response handlers.
xhr.onload = function (data) {
// Do stuff
};
而且最重要的一點
var dataJSON = JSON.stringify(dataARR);
xhr.send({ myJson: dataJSON });
但現在$_POST['myJson']
不存在
當我做了一個echo print_r($_POST);
,它返回Array()1
問:我需要發送xhr.send(????);
才能獲得它,以便我可以再次讀取php中的數組?