javascript - Issue getting first item from JSON array - Stack Overflow

$('#data').change(function () {$.ajax({url:'richiesta.php',type: 

$('#data').change(function () {
    $.ajax({
        url:        'richiesta.php',
        type:       'POST',
        dataType:   'json',
        data: {
            value: this.value
        },
    }).done(function (data) {
        $('#textfield').val(JSON.stringify(data));
        $('#results').val('Descrizione codice: ' + data[0].descrizione_codice);
    });
});

richiesta.php is just a file that triggers some functions to get the JSON. #textfield is correctly populated with the raw JSON, so everything is working properly.

I can't figure out how to output the first item of the JSON identified by the name descrizione_codice in #results.

The JSON is valid, here's an example selecting one option (truncated):

{
   "data":[
      {
         "codice_parto":"PRO",
         "descrizione_codice":"Competenze fisse per il personale a tempo indeterminato",
         "codice_siope":"1101",
         "descrizione_ente":"",
         "ricerca":false,
         "idtable":"000717409-1101",
         "cod_ente":"000717409",
         "anno":"2014",
         "periodo":"12",
         "codice_gestionale":"1101",
         "imp_uscite_att":"756",
         "importo_2013":"37718576",
         "importo_2014":"32810124",
         "importo_2015":null
      }
   ],
   "cosa":false
}

I'm doing something wrong is that data(0).descrizione_codice as Firebug tells me "data is not a function"..

I'm not using $.parseJSON because jQuery already parses data correctly thanks to the datatype.

I put up a test page here. You can request the JSON response selecting an option from the dropdown menu.

$('#data').change(function () {
    $.ajax({
        url:        'richiesta.php',
        type:       'POST',
        dataType:   'json',
        data: {
            value: this.value
        },
    }).done(function (data) {
        $('#textfield').val(JSON.stringify(data));
        $('#results').val('Descrizione codice: ' + data[0].descrizione_codice);
    });
});

richiesta.php is just a file that triggers some functions to get the JSON. #textfield is correctly populated with the raw JSON, so everything is working properly.

I can't figure out how to output the first item of the JSON identified by the name descrizione_codice in #results.

The JSON is valid, here's an example selecting one option (truncated):

{
   "data":[
      {
         "codice_parto":"PRO",
         "descrizione_codice":"Competenze fisse per il personale a tempo indeterminato",
         "codice_siope":"1101",
         "descrizione_ente":"",
         "ricerca":false,
         "idtable":"000717409-1101",
         "cod_ente":"000717409",
         "anno":"2014",
         "periodo":"12",
         "codice_gestionale":"1101",
         "imp_uscite_att":"756",
         "importo_2013":"37718576",
         "importo_2014":"32810124",
         "importo_2015":null
      }
   ],
   "cosa":false
}

I'm doing something wrong is that data(0).descrizione_codice as Firebug tells me "data is not a function"..

I'm not using $.parseJSON because jQuery already parses data correctly thanks to the datatype.

I put up a test page here. You can request the JSON response selecting an option from the dropdown menu.

Share Improve this question edited May 23, 2017 at 12:22 CommunityBot 11 silver badge asked Jan 4, 2015 at 18:37 MultiformeIngegnoMultiformeIngegno 7,06916 gold badges64 silver badges124 bronze badges 0
Add a ment  | 

2 Answers 2

Reset to default 4

According to your JSON structure you should be able to access array as data.data

.done(function (data) {
    console.log(data.data[0].descrizione_codice);
});
    $('#results').val('Descrizione codice: ' + data.data[0].descrizione_codice);
  1. First data is your actual js variable
  2. Second data is your array inside JSON named "data" where we access first element's property named "descrizione_codice"

Hope it clarifies

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745445687a4628035.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信