-1
阿賈克斯JSON請求我使用一個文件backend.php
沒有jQuery的
echo json_encode(array('post' => $_POST));
和JavaScript Ajax請求:
function ajax_(requst) {
requst = requst || {};
var result = {};
(function() {
var xhttp = window.XMLHttpRequest ? new XMLHttpRequest() :
new ActiveXObject("Microsoft.XMLHTTP");
xhttp.onreadystatechange = function() {
if (xhttp.readyState === 4 && xhttp.status === 200) {
result = JSON.parse(xhttp.responseText);
//result = (xhttp.responseText);
} else {
result = {
error: 'status="'+xhttp.statusText+' ('+xhttp.status+')", state="'+xhttp.readyState+'"'
};
}
};
xhttp.open(requst.type || 'POST', 'backend.php', false);
xhttp.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
//xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send(requst.data);
})();
return result;
}
而且還要求使用
console.log('before');
console.log(ajax_({
data: {
a: 1,
b: {
b1: [],
b2: {},
b3: 'b3'
}
}
}));
console.log('after');
如何做到這一點控制檯顯示
before
Object { post: a: 1, b: {b1: [], b2: [], b3: 'b3'}
after
?
問題是json發送不起作用。只有json從後端得到作品。
我們需要知道什麼是'$ _POST'之前,我們可以嘗試一個合理的答案。請在您的'backend.php'代碼中添加一個'print_r($ _ POST);'並將輸出添加到您的問題中。請勿將它添加爲註釋。 – RiggsFolly