devbridge / jQuery-Autocomplete

Ajax Autocomplete for jQuery allows you to easily create autocomplete/autosuggest boxes for text input fields
https://www.devbridge.com/sourcery/components/jquery-autocomplete/
Other
3.56k stars 1.66k forks source link

Suggestions is undefined #727

Closed rafaelluz-f closed 6 years ago

rafaelluz-f commented 6 years ago

Hi,

I'm having some trouble to use your plugin.

I read other issues and this error is related with how the json is received. I tried many variations with the json but still it not works.

This is my last code, using an array of another issue.

public function r_get_results(){
            $nonce = $_POST['nonce'];
            if ( ! wp_verify_nonce( $nonce, 'cmb_ajax_search_get_results' ) ) {
                die( json_encode( array('error' => __( 'Error : Unauthorized action' ))));
            }
            $data = array (
                'suggestions' => 
                array (
                    0 => 
                    array (
                        'value' => 'Guilherand-Granges',
                        'data' => '750',
                    ),
                    1 => 
                    array (
                        'value' => 'Paris 01',
                        'data' => '750',
                    ),
                ),
            );
            $json = json_encode($data);
            die();
        }

That returns this json:


{
    "suggestions": [
        {
            "value": "Guilherand-Granges",
            "data": "750"
        },
        {
            "value": "Paris 01",
            "data": "750"
        },
        {
            "value": "Paris 02",
            "data": "750"
        },
        {
            "value": "Paris 03",
            "data": "750"
        },
        {
            "value": "Paris 04",
            "data": "750"
        },
        {
            "value": "Paris 05",
            "data": "750"
        },
        {
            "value": "Paris 06",
            "data": "750"
        },
        {
            "value": "Paris 07",
            "data": "750"
        },
        {
            "value": "Paris 08",
            "data": "750"
        },
        {
            "value": "Paris 09",
            "data": "750"
        }
    ]
}

Is it the array structure correct? What i'm missing here? Thanks!

tkirda commented 6 years ago

Yes, data structure is correct. Look at the developer console to see where you getting an error.