簡體   English   中英

通過AJAX引入JSON以填充下拉列表

[英]Pulling in JSON via AJAX to populate a Drop-Down

我提取了一些會有所不同的JSON數據,例如:

返回的數據可能是:

[{"userID":"2779","UserFullName":" Absolute Pro-Formance"},{"userID":"2780","UserFullName":" AR Fabrication"},{"userID":"2781","UserFullName":" Banda Lucas Design Group"}]

要么:

[{"orderID":"112958","OrderName":"Order ID: 112958"},{"orderID":"112957","OrderName":"Order ID: 112957"},{"orderID":"112956","OrderName":"Order ID: 112956"}]

我正在嘗試做的是處理此JSON以構建<select>列表。

// Load in a drop-down as JSON
function LoadDropDown($url, $where, $id, $selectName){
    var $loading = '<div class="pageLoader" style="margin:0 auto !important;padding:0 !important;"><img src="/assets/images/ajax-loader.gif" alt="loading..." height="11" width="16" /></div>';
    var $t = Math.round(new Date().getTime() / 1000);
    var $container = jQuery($where);
    var options = {
            url: $url + '?_=' + $t,
            cache: false,
            type: 'POST',
            beforeSend: function(){
                    $container.html($loading);  
                },
            success: function(data, status, jqXhr){
                $html = '<select class="form-control" id="'+$selectName+'" name="'+$selectName+'">';
                $html += '<option value="0">- Select an Option -</option>';
                for(var i = 0; i < data.length-1; ++i) {
                    var item = data[i];
                    console.log(item.userID);
                }
                $html += '</select>';
                $container.html('<pre>' + data + '</pre>');
            },
            complete: function(jqXhr, status){},
            error: function(jqXhr, status, error){
                $container.slideDown('fast').html('<div class="alert alert-danger alert-dismissable"><button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><i class="fa fa-exclamation-triangle fa-4x pull-left"></i><p><strong>Danger Will Robinson!</strong><br />There was an issue pulling in this page. Our support team has been notified, please check back later.</p></div>');    
            }
    };
    jQuery.ajax(options);
}

我遇到的問題是...#1 console.log(item.userID); 總是顯示undefined ,並且#2如何有效地動態構建選項? 返回的JSON總是每row包含2個項目和id ,以及一個name

UPDATE

for(var $key in data){
    var $val = data[$key];
    for($j in $val){
        console.log('name:' + $j + ' = ' + $val[$j]);
    }
}

正在顯示我在Firefox控制台中需要的內容...但是每行有一項,每行(例如第一個JSON) name:userID = 1234下一行name:UserFullName = TheName

如何獲得它們,以便構建我的<options>

附:

for(var k in data) {
    console.log(k, data[k]);
}                   

我回來了:

2955 Object { orderID="8508", OrderName="Order ID: 8508"}

2955 Object { userID="1355", UserFulleName="Me Myself And I"}

您無需使用此類凌亂的代碼。 同樣在您的Ajax設置中dataType:"json"

success:function() {
    var listB=$('#yourdropdownId');
    listB.empty();  
    $.each(result, function (index, item) {
        listB.append(
            $('<option>', {
                value: item.userID,
                text: item.UserFullName
            }, '<option/>'))
         });
    }

如果只想從服務器檢索json,也可以使用$ .getJson而不是ajax

$.getJSON('@Url.Action(" "," ")',
                { "youparametername": yourdata}, function (data) {
                    $.each(data, function (index, item) {
                    })
            });

在options對象中,請確保使用

dataType: 'json'

或者,您可以在成功處理程序中使用

JSON.parse(data)

已治愈:更改了我的循環,以循環遍歷返回的JSON中的每個項目,獲取其密鑰等。

var $dl = data.length;
for(var $i = 0; $i < $dl - 1; ++$i) {
    var $keys = Object.keys(data[$i]);
    $html += '<option value="' + data[$i][$keys[0]] + '">' + data[$i][$keys[1]] + '</option>';
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM