Javascript POST变量始终保持为空
这是我使用jquery.ajax管理数据发送的方法。默认情况下,发送一个空字符串,每次更改时,我都会观察输入上的更改并重新发送。通过控制台,一切都很好,但在php中,Javascript POST变量始终保持为空,javascript,php,jquery,sql,ajax,Javascript,Php,Jquery,Sql,Ajax,这是我使用jquery.ajax管理数据发送的方法。默认情况下,发送一个空字符串,每次更改时,我都会观察输入上的更改并重新发送。通过控制台,一切都很好,但在php中,$this->searchField始终具有空字符串的值 function checkInfo(){ if(isset($_POST['searchField'])){ $this->searchField = json_decode($_POST['searchField']);
$this->searchField
始终具有空字符串的值
function checkInfo(){
if(isset($_POST['searchField'])){
$this->searchField = json_decode($_POST['searchField']);
if ($this->searchField != ""){
$this->query = "SELECT * FROM myguests WHERE firstName like '%".$searchField."%'";
}
else if ($this->searchField == "") {
$this->query = "SELECT * FROM myguests ORDER BY id";
}
$this->tableDisplay();
exit();
}
else{
return "error";
}
}
这是我的jquery ajax函数:
function searchGuests(users){
var data = {
"searchField": users
};
console.log(data);
$.ajax({
type: "POST",
dataType: "text",
url: "controllers/manageTable.php",
data: data
})
.done(function(tr) {
$("#tableBody").empty();
$("#tableBody").append(tr);
})
.fail(function(error){
console.log(error);
});
};
知道我遗漏了什么吗?在发送之前,尝试将用户编码为字符串
function searchGuests(users){
var data = {
"searchField": JSON.stringify(users);
};
console.log(data);
$.ajax({
type: "POST",
dataType: "text",
url: "controllers/manageTable.php",
data: data
})
.done(function(tr) {
$("#tableBody").empty();
$("#tableBody").append(tr);
})
.fail(function(error){
console.log(error);
});
};
用户变量包含什么?您没有发送任何JSON,因此不需要使用JSON_decode()没有类似于
JSON.encode()的方法,而且这个想法也没有意义。jQuery使用$.param()
通过默认值序列化对象我解决了它确实不需要json_decode-Ed将其与php json_encode混淆。这是JSON.Stringify,请分享您的答案。谢谢