JQuery自动完成返回内部服务器错误

时间:2017-05-02 23:48:28

标签: php jquery wordpress

我尝试使用wordpress进行jquery自动完成,但我不知道我的代码中的错误500在哪里:

我认为我的代码是用php。

   $('#search_type_fiche').autocomplete({
        source: function(search, response) {
            $.ajax({
                type: 'POST',
                dataType: 'json',
                url: MyAjax.ajaxurl,
                data: 'action='+MyAjax.action+'&search='+search.term,
                success: function(data) {
                    response($.map(data, function(item) {
                        return {
                            label: item,
                            value: item
                        }
                }));
                    //$("p#resultat").html(response(data));
                },
                error : function(XMLHttpRequest, textStatus, errorThrown) {
                    $("div#ajax-response").removeClass().addClass('error').html('error');
                } 
            });
            console.log(data);
        }
    });

回调php:

public $search;

    public function __construct()
    {
        global $wpdb;
        $this->wpdb = $wpdb;
    }

public function SearchTypeFiche($search){
        $this->search = $search;    

        $datas = $wpdb->get_results(
            $wpdb->prepare(
                "SELECT slug,id FROM {$this->wpdb->prefix}fiches_type WHERE slug LIKE %s",
                $this->esc_like(stripslashes($this->search)).'%'
            )
        );

        $results = array();
        foreach($datas as $key){
            $arr          = array();
            $arr['label'] = addslashes($key->slug);
            $arr['value'] = $key->id;
            $results[]    = $arr;
        };  
        return json_encode($results);       
    }

    $data_search = $_POST['search'];
    parent::SearchTypeFiche($data_search);

寻求帮助!

ps:我在stackoverflow中搜索但没有帖子解决我的问题..

1 个答案:

答案 0 :(得分:0)

我也试过,问题出在哪里? :

    public function search_type_fiche() {

    $search_data = $wpdb->get_results(
        $wpdb->prepare(
            "SELECT id,slug FROM {$wpdb->prefix}fiches_type WHERE slug LIKE %s",
            '%'.$_REQUEST['term'].'%'
        )
    );

    $suggestions=array();

    foreach ($search_data as $post):        
    $suggestion = array();
    $suggestion['label'] = $post->slug;
    $suggestion['link'] = $post->id;
    $suggestions[]= $suggestion;
    endforeach;

    echo $_GET["callback"] . "(" . json_encode($suggestions) . ")"; 



        die();
    }


$("#search_type_fiche").autocomplete({
        delay: 0,
        minLength: 3,
        source: function(req, response){  
            $.getJSON(MyAjax.ajaxurl+'?callback=?&action='+MyAjax.action, req, response);

        },
                select: function(event, ui) {
                    window.location.href=ui.item.link;
                },
        }
        );

为什么不工作?

Wordpress有这个并且工作正常:

<script type="text/javascript">
jQuery(document).ready(function ($){
    var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
        var ajaxaction = '<?php echo self::$action ?>';
        $("#secondary #searchform #s").autocomplete({
        delay: 0,
        minLength: 3,
        source: function(req, response){  
            $.getJSON(ajaxurl+'?callback=?&action='+ajaxaction, req, response);  
        },
                select: function(event, ui) {
                    window.location.href=ui.item.link;
                },
        });
 });
</script>

private function autocomplete_suggestions() {
    $posts = get_posts( array(
        's' => $_REQUEST['term'],
    ) );
    $suggestions=array();
    global $post;
    foreach ($posts as $post): 
    setup_postdata($post);
    $suggestion = array();
    $suggestion['label'] = esc_html($post->post_title);
    $suggestion['link'] = get_permalink();
    $suggestions[]= $suggestion;
    endforeach;
    echo $_GET["callback"] . "(" . json_encode($suggestions) . ")";  
    exit;
}