jQuery未捕获语法错误:输入的意外结束


jQuery Uncaught Syntax Error: Unexpected end of input

我使用$。Post ajax方法来运行返回数组的函数。当数组返回到$。Post方法得到错误:

未捕获语法错误:input

这是$。post方法

$.post(
     ajax_url('showResult'),{
         search_type:search_type,
         search_criteria:search_criteria
     },function(d){
       var ary = $.parseJSON(d);
});
这是我的ajax函数返回数组
function showResult()
{
    $jinput = JFactory::getApplication()->input;
    $search_type = $jinput->get('search_type','','STRING');
    $search_criteria = $jinput->get('search_criteria','','STRING');
    $search_type = trim($search_type);
    $search_criteria = trim($search_criteria);
    if(!empty($search_type))
    {
        if($search_type == "Search by Name")
        {
            $db = JFactory::getDbo();
            $query = "SELECT * FROM #__product";
            $db->setQuery($query);
            $db->query();
            $result = $db->loadAssocList();
            $result = json_encode($result);
            echo ($result);
            exit;
        }
    }
}

我该怎么做才能解决这个错误?

$result = json_encode($result);  

$result可能为空。

也许你的$return不是json.

试试这个

Debug the $return strcture.
<?php
//add this line before exit;
var_dump($result);
?>

<script type="text/javascript">
$.post(
ajax_url('showResult'),{
        search_type:search_type,
        zarch_criteria:search_criteria
},
function(d){
    console.log(d);
    //^
    //+------ add this line
    var ary = $.parseJSON(d);
});
</script>