在bootstrap 3.0中单击选项卡时,我有一个Ajax调用
以下是JS代码
$('[data-toggle="tab"]').click(function (e) {
var $this = $(this);
loadtab = $this.attr('href');
if(loadtab=="#odhis"){
var email = $("#email");
$.ajax({
type: "POST",
url: "../orders.php",
data: {'email':email.val()},
dataType: "json",
success: function(response){
console.log(response);
},
error: function(xhr, status, error){
console.log(xhr.responseText);
}
});
}
});
PHP文件orders.php
function getCustomOrderHistory(array $data){
$cstm = array();
if( !empty( $data ) ){
$trimmed_data = array_map('trim', $data);
$mail = mysqli_real_escape_string( $this->_con, $trimmed_data['email'] );
$checktable = "SELECT * from `orders` WHERE csmail='$mail'";
$resultcstm = mysqli_query($this->_con, $checktable);
while($row = mysqli_fetch_array($resultcstm)){
$cstm[] = $row;
}
}
return $cstm;
}
现在,当我print_r($cstm)
时,它会在PHP文件中打印结果。
但在JS中,成功响应是空的。
请帮忙吗?
答案 0 :(得分:2)
由于您的dataType: "json"
而不是return
只是对$cstm
数组和echo
进行编码,如下所示:
function getCustomOrderHistory(array $data){
$cstm = array();
if( !empty( $data ) ){
$trimmed_data = array_map('trim', $data);
$mail = mysqli_real_escape_string( $this->_con, $trimmed_data['email'] );
$checktable = "SELECT * from `orders` WHERE csmail='$mail'";
$resultcstm = mysqli_query($this->_con, $checktable);
while($row = mysqli_fetch_array($resultcstm)){
$cstm[] = $row;
}
}
$json = json_encode($cstm);
echo $json;
}