2017-01-09 7 views
0

jQueryオートコンプリートを設定するのは簡単ではありませんなぜ私はこれに奇妙なのですか?jQueryオートコンプリートwith Ajax Suggetionが返されない

Jquery Function

$("#location_suggetion").autocomplete({ 
    autoFocus: true, 
    minLength: 1, 
    source: function(request,response) { 
     $.ajax ({ 
      url: base_url+'data_check/get_location', 
      data: {term: request.term}, 
      dataType: "jsonp", 
      cache: false, 
      success: function(data) { 
       response($.map(data.suggestions, function(item) { 
        return { 
         label: item, 
         value: item 
        } 
       }));  
      } 
     }); 
    }, 
}); 

PHP File Code

$term = $this->input->get('term'); 
$query = $this->db->query('SELECT * FROM `tb_cities` WHERE name LIKE "'.$term.'%" LIMIT 0,6'); 
$results = $query->result(); 
$html = ''; 
$html.= '['; 
foreach($results as $result){ 
    $html.= '{ label: "'.$result->name.', '.get_country_row($result->country_id)->name.'", value: "'.$result->name.'" },'; 
} 
$html.='];'; 

echo $html; 

Console Get me Result

[{ label: "Rangat, India", value: "Rangat" },{ label: "Rajahmundry, India", value: "Rajahmundry" },{ label: "Rajamahendri, India", value: "Rajamahendri" },{ label: "Rajampet, India", value: "Rajampet" },{ label: "Rajendranagar, India", value: "Rajendranagar" },{ label: "Rajoli, India", value: "Rajoli" },]; 
+0

Jsonか? ajaxコールが送信され、ステータス200(OK)で返されますか? –

+0

コンソールにエラーがなく、正常に戻る@MoshFeu – ImBS

+1

jsonデータがコンソールで間違っています。 –

答えて

2

jsonpは、cross domainリクエストに使用されます。代わりにjsonを使用してください。コンソールのJSON日付が間違っていますhere。 JSON文字列で配列を変換するには、json_encodeを使用します。

$term = $this->input->get('term'); 
$query = $this->db->query('SELECT * FROM `tb_cities` WHERE name LIKE "'.$term.'%" LIMIT 0,6'); 
$results = $query->result(); 

$html= array();$i=0; 

foreach($results as $result){ 
    $html[$i]['label']=$result->name.', '.get_country_row($result->country_id)->name; 

    $html[$i]['value']=$result->name; 
$i++; 
} 
echo json_encode($html); 

これは、構造がすでに一致しているので、jqueryパートで変換する必要はありません。これは直接使用することができます。

+0

私は同じ出力を試みます – ImBS

+0

@BhavinSasapraとその出力は何ですか? –

+0

コンソールは出力を得て適切な結果を表示しますが、提案はされません。 – ImBS

0

私はこの問題は、PHPコードによって返さあなたのJSON形式であると思います。あなたがエコーしようとしている文字列を見てみると

<?php 
    require('connect.php'); 

    $getDetails = "SELECT id, concat(first_name,' ',last_name) as name FROM test.datatables_demo"; 
    $details = mysqli_query($con, $getDetails); 

    $response = array(); 
    if(mysqli_num_rows($details) > 0) 
    { 
     while($row = mysqli_fetch_assoc($details)) 
     { 
      $response[$row['id']] = $row['name']; 
     } 
    } 

    echo json_encode($response); 
?> 
+0

get me console error SyntaxError:位置3のJSONで予期しないトークンl – ImBS

1

<script> 
    $(function(){ 
     var arr = []; 
     var availableTags = []; 

     $.ajax({ 
      'url' : 'getdata.php', 
      'method': 'get', 
      success : function(response){ 
       arr = JSON.parse(response); 

       availableTags = Object.keys(arr).map(function (key) { return arr[key]; }); 
      }, 
      async : false 
     }); 

     $("#tags").autocomplete({ 
      source: availableTags 
     }); 
    }); 
    </script> 

PHPの:これを試してみてください。あなたはそれが有効なjsonではないことに気付くでしょう。それはあなたがまた、側面には正しい書式を作るかを決めることができますJSON

foreach($results as $result){ 
    $data[] = [ 
       "label" => $result->name.', '.get_country_row($result->country_id)->name, 
       "value" =>$result->name 
    ]; 
} 
//Now parse to json 
echo json_encode($data); 

にPHPの配列を解析するjson_encode機能を使用することができ、それを自分で作成Insteadofブラケット

// This is valid 
[ 
    { label: "Rajoli, India", value: "Rajoli" } 
] 

//this is invalid 
[ 
    { label: "Rajoli, India", value: "Rajoli" }, 
] 

前にカンマで終わり{"label": "some string", "value": "data"}

2番目の問題は、$.mapオブジェクトにラベルと値を設定しようとすると関連している可能性があります。

$("#location_suggetion").autocomplete({ 
    autoFocus: true, 
    minLength: 1, 
    source: function(request,response) { 
     $.ajax ({ 
      url: base_url+'data_check/get_location', 
      data: {term: request.term}, 
      dataType: "json", // This is right 
      cache: false, 
      success: function(data) { 
       response(data); // already mapped in backend 
      } 
     }); 
    }, 
}); 
+0

これがなぜ無効か教えてください。 – ImBS

+0

ちょうどjsonのrfcを指してhttps://tools.ietf.org/html/rfc7159 –

+0

ありがとう、私はこの形式で単純なソースを使用する場合、これは完全に実行されます。 – ImBS

0

CONVER Arrayコンソールですべてのエラー

$term = $this->input->get('term'); 
$query = $this->db->query('SELECT * FROM `tb_cities` WHERE name LIKE "'.$term.'%" LIMIT 0,6'); 
$results = $query->result(); 
$html = array(); 
foreach($results as $result){ 
    $html[]= array("label" => $result->name.",".get_country_row($result->country_id)->name, 
        "value" => $result->name); 
} 

echo json_encode($html,JSON_FORCE_OBJECT); 
+0

同じ出力を試してみます – ImBS

関連する問題