禁止访问JSON响应的值

时间:2015-11-21 04:53:38

标签: javascript jquery json ajax

我试图从我的json响应中访问所需的值,以便加载到下拉框中。但我得到undefined作为下拉项目。

以下是我的json回复:

{"currency":[{"code":"USD","dateModified":"2015-10-30T20:43:58","decimalPlace":"2","id":1,"status":true,"symbolLeft":"$","symbolRight":"","tiendayaOrderses":[],"title":"US Dollar","value":1.00000000},{"code":"EUR","dateModified":"2015-10-30T20:43:58","decimalPlace":"2","id":2,"status":true,"symbolLeft":"","symbolRight":"€","tiendayaOrderses":[],"title":"Euro","value":0.76775432},{"code":"GBP","dateModified":"2015-10-30T20:43:58","decimalPlace":"2","id":3,"status":true,"symbolLeft":"£","symbolRight":"","tiendayaOrderses":[],"title":"Pound Sterling","value":0.64524455}]}

以下是我获取回复的方式:

$(document).ready(function() {
    $.ajax({
        url: "currencies",
        dataType: 'json',
        error: function() {
            alert('error');
        },
        success: function(data) {
            //var jsonData = $.parseJSON(data);
            $.each(data.currency, function(index) {
                $.each(data.currency[index], function(key, value) {
                    //curncyDrop is the drop-down box
                    $('#curncyDrop').append('<li><a>' + value.title +
                        '</a></li>');


                });

            });
        }

    });
});

这会将所有数据加载到下拉列表中,而不是加载所需的值( title )值:

$('#curncyDrop').append('<li><a>' + value + '</a></li>');

提前致谢。

3 个答案:

答案 0 :(得分:2)

var data = {
  "currency": [{
    "code": "USD",
    "dateModified": "2015-10-30T20:43:58",
    "decimalPlace": "2",
    "id": 1,
    "status": true,
    "symbolLeft": "$",
    "symbolRight": "",
    "tiendayaOrderses": [],
    "title": "US Dollar",
    "value": 1.00000000
  }, {
    "code": "EUR",
    "dateModified": "2015-10-30T20:43:58",
    "decimalPlace": "2",
    "id": 2,
    "status": true,
    "symbolLeft": "",
    "symbolRight": "€",
    "tiendayaOrderses": [],
    "title": "Euro",
    "value": 0.76775432
  }, {
    "code": "GBP",
    "dateModified": "2015-10-30T20:43:58",
    "decimalPlace": "2",
    "id": 3,
    "status": true,
    "symbolLeft": "£",
    "symbolRight": "",
    "tiendayaOrderses": [],
    "title": "Pound Sterling",
    "value": 0.64524455
  }]
}



$.each(data, function(index, value){
$.each(value, function(indx, val){
$('#curncyDrop').append('<li><a>' + val.title + '</a></li>');
  $('#curncyDrop').append('<li><a>' + val.value + '</a></li>');
})
})

  
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul id='curncyDrop'></ul>

试试这个

答案 1 :(得分:1)

我认为你有一个不必要的循环,只要你的数据是有效的JSON并且title属性存在,你应该能够做到以下几点:

$(document).ready(function () {
    $.ajax({
        url: "currencies",
        dataType: 'json',
        error: function () {
            alert('error');
        },
        success: function (data) {
            $.each(data.currency, function (key, value) {
                $('#curncyDrop').append('<li><a>' + value.title +'</a></li>');
            });
        }
    });
});

如果它不是JSON对象,那么使用JSON.parse(数据),如果您仍然收到未定义的错误,我建议使用console.log(数据)来进一步调查。

答案 2 :(得分:1)

试试这个:

    var currencyData = data.currency;
    $.each(currencyData, function(key, value){
       //curncyDrop is the drop-down box
       $('#curncyDrop').append('<li><a>' + value.title +'</a></li>');
    });