2016-04-28 6 views
1

グループで選択した2つの検索ajaxの結果を表示しようとしています。しかし、結果は表示されません。私はこれにWordPress Ajaxを使用しています。私は予想通りこれが実行されていないグループ2でajaxの結果を選択

$searchString = $_POST['q']; 
$childdata = array(); 

$query = new WP_Query(array('s' => $searchString)); 

if ($query->have_posts()) { 
    while ($query->have_posts()) { 
     $query->the_post(); 
     $title = get_the_title(); 
     $ID = get_the_id(); 
     $childdata[] = array('id' => "post-".$ID, 'text' => $title); 
    } 
} else { 
    $data[] = array('id' => '0', 'text' => 'No results Found'); 
} 

$data = array(
    "text" => "posts", 
    "children" => $childdata 
); 

wp_reset_postdata(); 

// return the result in json 
echo json_encode($data); 
die(); 

、としてPHPから戻ってい

ここに私のJSコードは、

jQuery('select.select2-group_filters-dropdown').select2({ 
      //placeholder: "Select pages/post/categories", 

      ajax: { 
       url: ajaxurl, 
       dataType: 'json', 
       method: 'post', 
       delay: 250, 
       data: function (params) { 
        return { 
         q: params.term, // search term 
         page: params.page, 
         action: 'cp_get_posts_by_query' 
        }; 
       }, 
       results: function (data, page) { 
         return {results: data}; 
       }, 
       processResults: function (data) { 

        return { 
         results: data 
        }; 
       }, 
       cache: true 
      }, 
      minimumInputLength: 0, 

     }); 

データ。結果はゼロです。これで私を助けてください。

+0

トライますprint_r(json_encode($データ));エコーの代わりに –

+0

それも試してみました。動作しません。私のJSONの結果は '{" text ":" posts "、" children ":[{" id ":" post 39 "、" text ":" Nihil voluptatem provent reprehenderit et voluptatem rerum "}、{" id " : "post-2"、 "text": "Sample Page"}、{"id": "99"、 "text": "Rerum quasi odio sed"}、{"id" 、 "text": "Nesciunt iste doloribus exercitationem eligendi"}、{"id": "post-104"、 "text": "Praesentium et dolorem excepturi voluptatibus reiciendis"}、{"id": "post-81" "text": "エラーコーディネーター"}、{"id": "post-63"、 "text": "et sequi enim delectus"}} ' – patilvikasj

+0

あなたはデータを受け取ったconsole.logを試しましたか?それはコンソール上で0を与えますか、まったく何も与えませんか? –

答えて

0

バックエンドからデータを取得する場合、問題はselect2構成にあります。 最初にajaxを呼び出してから、select2にデータを入力してみてください。この(それはあなたのために働くだろう、私はここでそれをテストすることはできませんかわからない)のようなSomethong:

jQuery.ajax({ 
url: ajaxurl, 
dataType: 'json', 
method: 'post', 
delay: 250, 
data: function (params) { 
    return { 
     q: params.term, // search term 
     page: params.page, 
     action: 'cp_get_posts_by_query' 
    } 
    } 
    }).done(function(data) { 

jQuery('select.select2-group_filters-dropdown').select2({ data:data, minimumInputLength: 0}); 


    }); 
-1
$('.select2').select2({ 
       allowClear: true, 
       ajax: { 
        url: function (params) { 
         return "api/endpoint/?user=" + params.term; 
        }, 
        dataType: 'json', 
        delay: 500, 
        processResults: function (data) { 
         return { 
          results: $.map(data, function (item) { 
           return { 
    /* NOTE: return in this format i.e. 
    * key = **text** : value = username 
    * key = **id** : value = id 
    */ 
            text: item.username, 
            id: item.id 
           } 
          }) 
         }; 
        }, 
        minimumInputLength: 1, 
        minimumInputLength: 3, 
        cache: true, 
        escapeMarkup: function (markup) { 
         return markup; 
        }, 
        templateResult: function (item) { 
         return item.username; 
        }, 
        templateSelection: function (item) { 
         return item.username; 
        }, 
       } 
      }); 
関連する問題