简体   繁体   English

如何从JSON项目绑定到下拉值

[英]How to bind into a dropdown values from a json item

Im a having a long night here 我在这里度过了一个漫长的夜晚

The result of the webservice is in json. Web服务的结果在json中。 However data returned comes with diagnosic data. 但是,返回的数据带有诊断数据。 How do I get the result set only? 如何仅获得结果集?

Here is my code: 这是我的代码:

http://jsfiddle.net/bkyg23cx/ http://jsfiddle.net/bkyg23cx/

 $(document).ready(function () { $.ajax( { type: "POST", contentType: "application/json; charset=utf-8", url: "http://website.com", data: data, dataType: "json", success: function (data) { alert('Total results found: ' + data.result.total); $.each(data.result.records, function (value) { $("[id$='uxRegion']").append($("<option></option>").val(value.province).html(value.province)); }); } error: function ajaxError(response) { alert(response.status + ' ' + response.statusText); } }); }); 

Here is the problematic json value 这是有问题的json值

 { "value1": "etetetetete", "value2": true, "result": { "records": [ { "province": "Star world" }, { "province": "CNN" } ], "fields": [ { "type": "text", "id": "province" } ], "hash": "hash value" } } 

You can set data.result.records as a local variable records , just like below: 您可以将data.result.records设置为局部变量records ,如下所示:

success: function (data) {
    var records = data.result.records;

    alert('Total results found: ' + records.length);

    $.each(records, function (value) {
        $("[id$='uxRegion']").append($("<option></option>")
                             .val(value.province)
                             .html(value.province));
    });
}

And You don't have total property on result object, you should use .length . 而且您对结果对象没有total属性,应该使用.length

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM