2014-09-22 160 views
-2

您好,請我無法從中通過ajax返回以下陣列獲取數據:獲取從JSON數組數據

Array 
(
    [routes] => Array 
     (
      [0] => Array 
       (
        [route_id] => 4 
        [company_id] => 2 
        [from] => Soye 
        [to] => Martelange 
        [fare] => 98 
        [is_active] => 
       ) 

      [1] => Array 
       (
        [route_id] => 9 
        [company_id] => 2 
        [from] => Auckland 
        [to] => Stevoort 
        [fare] => 65 
        [is_active] => 
       ) 

      [2] => Array 
       (
        [route_id] => 11 
        [company_id] => 2 
        [from] => Bowden 
        [to] => Kessel 
        [fare] => 60 
        [is_active] => 
       ) 

      [3] => Array 
       (
        [route_id] => 17 
        [company_id] => 2 
        [from] => Berg 
        [to] => Clearwater Municipal District 
        [fare] => 97 
        [is_active] => 
       ) 

      [4] => Array 
       (
        [route_id] => 24 
        [company_id] => 2 
        [from] => Martelange 
        [to] => Soye 
        [fare] => 98 
        [is_active] => 
       ) 

      [5] => Array 
       (
        [route_id] => 29 
        [company_id] => 2 
        [from] => Stevoort 
        [to] => Auckland 
        [fare] => 65 
        [is_active] => 
       ) 

      [6] => Array 
       (
        [route_id] => 31 
        [company_id] => 2 
        [from] => Kessel 
        [to] => Bowden 
        [fare] => 60 
        [is_active] => 
       ) 

      [7] => Array 
       (
        [route_id] => 37 
        [company_id] => 2 
        [from] => Clearwater Municipal District 
        [to] => Berg 
        [fare] => 97 
        [is_active] => 
       ) 

     ) 

) 

這是我的javascript:

var data = {id:id}; //Array 

     $.ajax({ 
      url : url+"ticketinfo/routes", 
      type: "POST", 
      data : data, 
      success: function(data, textStatus, jqXHR) 
      { 
       var jsonArray = data.routes; 
       var options = $("#options"); 
       $.each(jsonArray , function(index, data) { 
        //adds all this values fron the array ,only from and to 

        options.append($("<option/>").text(data.from + "/" + data.from)); 
       }); 
      }, 
      error: function (jqXHR, textStatus, errorThrown) 
      { 

      } 
     }); 

而且通過麻煩我的意思是我不知道如何,我在這裏嘗試了幾個例子,但沒有爲我工作。我想要做的就是在數組中獲取這兩個字段:從和到使用該數據動態地使用jquery動態填充兩個選擇控件。如果有人能幫我解決這個問題,我將非常感激。先謝謝你。 :)

回答

-1

這是我該怎麼做,你有沒有嘗試下面的方法來檢查輸出到你的控制檯作爲測試結果?

var routes = data.routes 
for(var i = 0; i < routes.length; i++){ 
    console.log(routes[i].to); 
    console.log(routes[i].from); 
} 
+0

類型錯誤:路由未定義 for(var i = 0; i 2014-09-22 21:45:31

-1

試試這個:

var selectData1 = ""; 
var selectData2 = ""; 
routes.each(function(index,options) 
{ 
    selectData1 += '<option>' + $(this)[0].from + '</option>'; 
    selectData2 += '<option>' + $(this)[0].to + '</option>'; 

}); 

    $("#select1ID").html(selectData1); 
    $("#select2ID").html(selectData2); 

我還沒有嘗試過這樣做期待一些語法錯誤,但是這應該做你需要的工作。

0

顯然,這一切都與json數組的編碼和解碼有關。 Csdtestings回答沒有奏效的原因是他在他的例子中使用的數組不是我認爲的json編碼的對象數組。無論如何,我嘗試使用json.parse返回的數組擺脫了錯誤,但後來我有另一個問題是數組元素都顯示爲「[object] [object]」。那麼它發生在我身上,它可能與編碼的數組類型有關。所以,我回到我的php和改變了這一點:

$data = $this->route->get_routes_by_company($company_id); //using codeigniter framework 
echo json_encode($data); 

要這樣:

$data = $this->route->get_routes_by_company($company_id); //using codeigniter framework 
foreach ($data as $row) 
{ 
    $result = array($row['from'], $row['to']); 
} 
echo json_encode($result); 

之後,我只是改變了我的javascript來此,瞧它的工作:

$.ajax({ 

    type: "POST", 
    url: url+"ticketinfo/routes", 
    data: { 
     'id' : id 
    }, 
    dataType: "json", 
    success: function (response) { 
     if (response.length > 0) 
     { 
      $.each(response, function(index, element) { 
       $('#From').append("<option value='" + element[0] + "'>" + element[0] + "</option>"); 
       $('#To').append("<option value='" + element[1] + "'>" + element[1] + "</option>");   
      }); 
     } 
    } 

});