javascript - POST 变量始终为空
问题描述
这是我使用 jquery.ajax 管理数据发送的方法。默认情况下发送一个空字符串,每次更改时我都会观察输入的更改并重新发送。通过控制台事情很好,但在 php 中$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);
});
};
推荐阅读
- javascript - 如何通过令牌使路由器访问
- java - 使用 Navigation 组件获取任何片段实例
- mysql - 当我将它与完全连接一起使用时,SQL table.column 不起作用
- testing - 强制 testcafe 使用 vpn 访问网页的特定功能
- amazon-web-services - 如何在没有 Application Load Balancer 的情况下将流量路由到 ECS Fargate 实例
- java - 我应该使用什么 proguard 规则来使 javaMailApi 完美工作?
- ruby - RSpec:对类似的 it 块使用 each 循环
- reporting-services - Visual Studio 2012 中的 Neodynamic QR 码报告无法付款 (UPNQR)
- ruby-on-rails - 错误:nil:NilClass 的未定义方法“年份”
- python - 如何在熊猫的数据框中获得具有某些条件的先前值