Php 使用AJAX基于POST数据从数据库中获取数据

Php 使用AJAX基于POST数据从数据库中获取数据,php,ajax,joomla,Php,Ajax,Joomla,我正试图根据POST数据从db中获取数据,但出现了一些奇怪的错误: Uncaught SyntaxError: Unexpected token < in JSON at position 0 Uncaught SyntaxError:JSON中位置0处的意外标记< 我很确定我的数据查询出了问题——语法错误/类型错误,但我不能var_转储变量值,所以我有点不知所措 <?php defined('_JEXEC') or die; $data = []; $ajax_test =

我正试图根据POST数据从db中获取数据,但出现了一些奇怪的错误:

Uncaught SyntaxError: Unexpected token < in JSON at position 0
Uncaught SyntaxError:JSON中位置0处的意外标记<
我很确定我的数据查询出了问题——语法错误/类型错误,但我不能var_转储变量值,所以我有点不知所措

<?php 
defined('_JEXEC') or die;

$data = [];
$ajax_test = [];

if(isset($_POST['checkboxList'])){
$data = $_POST['checkboxList']; // Possible results: 1; 12; 123 (checkbox value)
}
if(isset($_POST['thisisajax'])){
$ajax_test = $_POST['thisisajax'];
}

if($ajax_test){
 $db = JFactory::getDbo();
 $query = $db->getQuery(true);
 $query->select('*');
 $query->from($db->quoteName('#__products'));
 $query->where($db->quoteName('id') . ' = (', implode(",",$data));
 // var_dump($query->__toString()); Doesnt work... Why?
 $db->setQuery($query);
 $results = $db->loadObjectList();
 $response = $data;

echo new JResponseJson($response);
jexit();
}

?>

<form method="post" action="" name="form1" id="mycustomform">
<input type="checkbox" name="checkboxList[]" onclick="function1();" value="1"> Apple</input><br>
<input type="checkbox" name="checkboxList[]" onclick="function1();" value="2"> Pineapple</input><br>
<input type="checkbox" name="checkboxList[]" onclick="function1();" value="3"> Pen</input>
</form>
<div id="response"></div>

<script>
function function1() {
var data = jQuery("#mycustomform").serialize() + '&thisisajax=1';
console.log(data);
jQuery.ajax({
url: "<?php echo JUri::current(); ?>", 
type: "POST",
data: data, 
success: function(response){ 
response = jQuery.parseJSON(response);
if(response.success === true){
jQuery('#response').html(response.data);
} else {
alert('error');
}
}
});
}
</script>

苹果
菠萝
笔 函数function1(){ var data=jQuery(“#mycustomform”).serialize()+'&thisisajax=1'; 控制台日志(数据); jQuery.ajax({ url:“”, 类型:“POST”, 数据:数据, 成功:功能(响应){ response=jQuery.parseJSON(response); 如果(response.success==true){ jQuery('#response').html(response.data); }否则{ 警报(“错误”); } } }); }
试试这个
数据:JSON.stringify(数据)在开发人员控制台中检查脚本的原始输出您仍然可以使用var_dump。你进入chrome inspector的网络选项卡,点击xhr,它会有一个响应选项卡,在那里你可以看到服务器响应在开发人员控制台中检查脚本的原始输出您仍然可以使用var_dump。您进入chrome inspector的网络选项卡并单击xhr,它将有一个响应选项卡,您可以在其中看到服务器响应。