Ajax - outputting value from API

0 votes
203 views
added Jul 4, 2018 in API by LC Marshal Captain (25,790 points)
edited Jul 13, 2018 by LC Marshal
//JSON string
[
{
"Nid": "20",
"cost_to_own": "200000",
"list_price": "1000000",
"post_date": "Thursday, June 7, 2018 - 11:38",
"status": "1",
"size": "950 sqft",
"level": "10th Floor",
"unit_type": "Type A",
"updated_date": "<em class=\"placeholder\">23 hours</em> ago",
"roomtype": "2 BD / 2 BR",
"actual_price": "1000000",
"car_park": "2",
"unit_status": "2",
"title": "S-10-16"
},
{
"Nid": "24",
"cost_to_own": "250000",
"list_price": "1250000",
"post_date": "Thursday, June 7, 2018 - 14:38",
"status": "1",
"size": "1,000 sqft",
"level": "11th Floor",
"unit_type": "Type A",
"updated_date": "<em class=\"placeholder\">23 hours</em> ago",
"roomtype": "3 BD / 3 BR",
"actual_price": "1250000",
"car_park": "2",
"unit_status": "2",
"title": "S-B-2"
}
]

//unit_status value to output the label on front end
0|Coming Soon
1|Unfunded
2|Buying Closed. Now Funding
3|Funded
4|Closed

JSON tree viewer with https://codebeautify.org/jsonviewer
object {1} 
array [2]
0 {14} //property
Nid : 20 //name & value
cost_to_own : 200000
list_price  : 100000
post_date  : Thursday, June 7, 2018 - 11:38
unit_status : 2

//ajax call for API 
$(function() { 
  //get API
  var propertyApi = 'https://test.lazacode.org/api/property-units.json?property_nid=',
  apiValue = '25',
  fullApiLink = propertyApi + apiValue;

  $.ajax({
  url: fullApiLink,
  dataType: 'json',
  type: 'get',
  cache: 'false',
  success: function(data) {
  // accessing property "0"
    $(data["0"]).each(function(index, row) {
    //declare unit_status value from "0" property
    var unitStatus = (data["0"].unit_status);
    //condition for status
    if (unitStatus === '2') {
    $('#main_panel .listing-batch.hidden-sm.hidden-xs').html('Buying Closed. Now Funding');
    } else if (unitStatus === '' ) {
    console.log('no status')
    }
    });
  }
  })
});
lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...