Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse JSON in Ajax Call

I have this JSON response from the query from the database:

[{
  "IMAGE": "",
  "NAME": "BEANS,LIMA,DRY",
  "NSN": " 8915005302173",
  "FIIG": "T113-J",
  "INC": "2153",
  "CRITICALY": "0",
  "TYPE_II": "4",
  "DEMIL": "a",
  "DATE": "2003-06-12",
  "ESD": ")",
  "PMIC": "a",
  "ADPEC": "0",
  "HMIC": "n",
  "HCC": "",
  "ENAC": "",
  "SUPPLIER": "Defense Supply Agenc",
  "CAGE": "54027",
  "PART_NUMBER": "0",
  "STAT": "a",
  "RNCC": "3",
  "RNVC": "1",
  "DAC": "4",
  "RNAAC": "zz",
  "SADC": "",
  "MSDS": "",
  "MOE": "ds",
  "USC": "i",
  "AAC": "h",
  "SOS": "sms",
  "UI": "lb",
  "UNIT_PRICE": "1",
  "QUP": "1",
  "CIIC": "u",
  "SLC": "0",
  "MGT_CTRL": "",
  "REP": "",
  "SUPPLY_PACKAGE_TYPE": "Fruits and Vegetables",
  "SPECIAL_FEATURES": "",
  "DEFINITION": "Note-Subsistance items which are specifically prepared for dietetic use are classified in Class 8940. Nondietetic foods, even though they bear the same approved item names as corresponding dietetic fo"
}]

I want to select just the NSN number. This is the Ajax call:

$(document).ready(function(){
function show(){
    $.ajax({
        url:"getProducts",

        success:function(data){
            var d=data;
            $("#output").html(d);
        }
    });
}
show();
});
like image 204
Camelia Avatar asked Apr 08 '26 06:04

Camelia


1 Answers

Tell the $.ajax() method to treat your returned data as JSON by adding dataType :'json'. As your data will then be an object that has an array, which has an object at the first index, you will access your properties with data[0].PROPERTY. Example:

 $.ajax({
    url:"getProducts",
    dataType: 'json',
    success:function(data){
        var d=data[0];
        $("#output").html(d.NSN);
    }
});
like image 121
Ohgodwhy Avatar answered Apr 10 '26 19:04

Ohgodwhy