Are you first timer at lazacode.org? Why not join us and start exchange your programming knowledge with everyone. Register today!

jQuery - getJSON for API

0 votes
79 views
added May 28, 2018 in API by LC Marshal Captain (23,720 points)
edited Jul 12, 2018 by LC Marshal
var input = $('#keyword'),
    form =  $('#search'),
    apiurl = ('http://api.giphy.com/v1/gifs/search?api_key=dc6zaTOxFJmzC&q=' + '')

$.getJSON( apiurl + userInput, function( response ){
console.log(get.JSON)
})

9 Responses

0 votes
responded May 28, 2018 by LC Marshal Captain (23,720 points)
edited Jul 12, 2018 by LC Marshal
//test the getJSON is in place with console.log
var lookforstate = $('ol.breadcrumb li:last-child').text(),
    trimstate = lookforstate.split(","),
    resultstate  = trimstate [trimstate.length-1],
    getstate = $.trim(resultstate),
    outlookLislnk = 'https://alice.edgeprop.my/property/v1/nsearch?&state=',
    incDistrc = '&size=4&order_by=posted_desc&listing_type=sale&district=',
    fulloutLislink = outlookLislnk + getstate + incDistrc + areaname;

//API 
$.getJSON(fulloutLislink, function(areaOutlooklist) {
  console.log('it worked!');
});

 

0 votes
responded May 28, 2018 by LC Marshal Captain (23,720 points)
edited Jul 12, 2018 by LC Marshal
//Show value of JSON with console.log(value)
var areaname = $('h1.page-header').text(), 
    lookforstate = $('ol.breadcrumb li:last-child').text(),
    trimstate = lookforstate.split(","),
    resultstate  = trimstate [trimstate.length-1],
    getstate = $.trim(resultstate),
    outlookLislnk = 'https://alice.lalala.my/property/v1/nsearch?&state=',
    incDistrc = '&size=4&order_by=posted_desc&listing_type=sale&district=',
    fulloutLislink = outlookLislnk + getstate + incDistrc + areaname,

$.ajax({
  url: fulloutLislink,
  dataType: 'json',
  type: 'get',
  cache: 'false',
  success: function(data) {
    $(data.property).each(function(index, value) { //property based on the inspect element
      console.log(value);
    });
  }
})
0 votes
responded May 30, 2018 by LC Marshal Captain (23,720 points)
edited Jul 12, 2018 by LC Marshal
//To get value for array in JSON
//inspect element, go to value and right click to 'copy property path'
field_prop_bathrooms:{und: Array(1)}

var property_data = '';
$.each(areaOutlooklist.property, function(index, row) {
property_data += '<div class="col-md-3">'; 
property_data += '<div class="col-md-12">'; 
property_data += '<span class="single-card"><span class="title"><a href="'+'/listing/'+row.url+'">'+row.title+'</a></span>';   
property_data += '<span class="meta">'+row.district+', '+row.state+'</span>';
property_data += '<span class="price">'+row.field_prop_asking_price.und["0"].value+'</span></span>';
property_data += '</div>';
property_data += '</div>'; 
});
0 votes
responded May 30, 2018 by LC Marshal Captain (23,720 points)
edited Jul 12, 2018 by LC Marshal
//Get total JSON record count
$.getJSON(fulloutLislink, function(areaOutlooklist) { 

  var property_data = '';
  $.each(areaOutlooklist.property, function(index, row) {
    property_data += '<span class="single-card"><span class="title"><a href="'+'/listing/'+row.url+'">'+row.title+'</a></span>';   
    property_data += '<span class="meta">'+row.district+', '+row.state+'</span></span>';
  });
  alert(property_data.length);
});
0 votes
responded May 31, 2018 by LC Marshal Captain (23,720 points)
edited Jul 12, 2018 by LC Marshal
//Accessing JSON array to get count result i.e .found
var areaname = $('h1.page-header').text(),
    lookforstate = $('ol.breadcrumb li:last-child').text(),
    trimstate = lookforstate.split(","),
    resultstate  = trimstate [trimstate.length-1],
    getstate = $.trim(resultstate),
    outlookLislnk = 'https://alice.edgeprop.my/property/v1/nsearch?&state=',
    incDistrc = '&size=4&order_by=posted_desc&listing_type=sale&district=',
    fulloutLislink = outlookLislnk + getstate + incDistrc + areaname;

//API 
$.getJSON(fulloutLislink, function(areaOutlooklist) {
var property_data = '';
$.each(areaOutlooklist.property, function(index, row) {
  property_data += '<span class="meta">'+row.district+', '+row.state+'</span>';
});
var outlookListing    = $('#outlook-property-listing').fadeIn(500).append(property_data),

$('.count-sale').fadeIn(300).text(areaOutlooklist.found);
});
0 votes
responded Jun 1, 2018 by LC Marshal Captain (23,720 points)
edited Jul 12, 2018 by LC Marshal
<!--ad list of JSON object into HTML element e.g <ul>-->
<ul id="newlaunch-development-listings"> 
</ul>

<script>
  var lookforstate = $('ol.breadcrumb li:last-child').text(),
      trimstate = lookforstate.split(","),
      resultstate  = trimstate [trimstate.length-1],
      getstate = $.trim(resultstate),
      outlookLislnk = 'https://alice.edgeprop.my/property/v1/nsearch?&state=',
      newlaunchList = '&new_launch=1&district=',
      fulloutLisNewlaunch = outlookLislnk + getstate + newlaunchList + areaname;

  //newlaunches - upcoming development
  $.getJSON(fulloutLisNewlaunch, function(areaOutlookNewlaunchList) {
  var property_nldata = '';
  $.each(areaOutlookNewlaunchList.property, function(index, row) {   
    property_nldata += '<li><a target="_blank" href="'+'/listing/'+row.url+'">'+row.title+'</a></li>';    
  });

  $('ul#newlaunch-development-listings').fadeIn(500).append(property_nldata);
  });
</script>
0 votes
responded Jun 29, 2018 by LC Marshal Captain (23,720 points)
edited Jul 12, 2018 by LC Marshal
//Convert JSON Data to HTML
$(document).ready(function() { 
  $("#getMessage").on("click", function() {
    $.getJSON("/json/cats.json", function(json) {
    var html = "";   
    json.forEach(function(val) {
      var keys = Object.keys(val);
      html += " &amp;lt;div class = 'cat'&amp;gt;";
      key.forEach(function(key) {
        html += "&amp;lt;b&amp;gt;" + key + "&amp;lt;/b&amp;gt;: " + val[key] + ""; 
      });
      html += "&amp;lt;/div&amp;gt;";
    });
    $(".message").html(html);
    });
  });
});
0 votes
responded Jun 29, 2018 by LC Marshal Captain (23,720 points)
edited Jul 12, 2018 by LC Marshal
//Get JSON with the jQuery getJSON Method
$(document).ready(function() {
  // Only change code below this line.
  $("#getMessage").on("click", function(){
     $.getJSON("/json/cats.json", function(json){
     $(".message").html(JSON.stringify(json));
    });
  });
});
0 votes
responded Apr 16 by LC Marshal Captain (23,720 points)
var ptype = '';
  var select_asset = 0;
  var select_state = '';
  function show(aval) {
    if (aval == "l") {
        ptype = 'l';
        $('#hiddenLanded').slideToggle();
        $('#hiddenNonLanded').slideUp();
        $("#address-landed").val("");

        
        // Instant search landed
        $("#address-landed").keyup(function(e){
        var q = $("#address-landed").val();
        $.getJSON("https://lazacode.org/index.php?q=erp/autocomplete/l/"+q,
        
        function(data) {
          $("#results").empty();
          
          $.each(data, function(i,item){
            //item is basically the json response value; i = key, item = value
            var item_new = item.replace(", "," ");
            item_new = item_new.replace(", "," ");
            var expr_main = q;//expression to search
            var expr_upper = q.toUpperCase();

          
            if( q && (item_new.includes(expr_upper) || item_new.includes(expr_main))  ){
                var x = i.split(">>");
                var y = x[1].split("/");

                $("#results").append("<li class='list-search' data-state='"+y[0]+"' data-asset='"+y[1]+"'>" + item + "</li>");
                $("#results").css("display", "block");

                //clicking list to set input
                $(".list-search").click(function(){
                    $("#address-landed").val($(this).text());
                    select_state = $(this).attr('data-state');
                    select_asset = $(this).attr('data-asset');
                    $("#results").css("display", "none");
                });
                //hide div when mouse outside the div
                $("#results").mouseleave(function() {
                       $(this).css("display", "none");
                });

            }
            
          });
        });
      });
    }
    else if (aval == "r") {
        ptype = 'r';
        $('#hiddenNonLanded').slideToggle();
        $('#hiddenLanded').slideUp();
        $("#address-nonlanded").val("");


        //instant search non landed
        $("#address-nonlanded").keyup(function(e){
        var q = $("#address-nonlanded").val();

        $.getJSON("https://lazacode.org/index.php?q=erp/autocomplete/r/"+q,
        function(data) {
          $("#nonlanded-results").empty();
          
          $.each(data, function(i,item){
          
          
            //item is basically the json response value; i = key, item = value
            var item_new = item.replace(", "," ");
            item_new = item_new.replace(", "," ");
            var expr_main = q;
            var expr_upper = q.toUpperCase();
          
            if( q && (item_new.includes(expr_upper) || item_new.includes(expr_main))){
                var x = i.split(">>");
                var y = x[1].split("/");
              
                $("#nonlanded-results").append("<li class='list-search' data-state='"+y[0]+"' data-asset='"+y[1]+"'>" + item + "</li>");
                $("#nonlanded-results").css("display", "block");

                //clicking list to set input
                $(".list-search").click(function(){
                    $("#address-nonlanded").val($(this).text());
                    select_state = $(this).attr('data-state');
                    select_asset = $(this).attr('data-asset');
                    $("#nonlanded-results").css("display", "none");
                });

                //for hiding the div when mouse is outside of the div
                $("#nonlanded-results").mouseleave(function() {
                       $(this).css("display", "none");
                });

            }
            
          });
        });
      });

    } 
    else{
     $('#hiddenLanded, #hiddenNonLanded').slideUp();
    }
  }
  $(document).ready(function(){

    $('#ReportSubmit').click(function(){

      var asset = select_asset;   
      var property_type = $('#property_type option:selected').val();
      var sub_type = $('#sub_type option:selected').val();
      var block = $('#block').val();
      var level = $('#level').val();
      var built_up = $('#built_up').val();
      var built_up_nl = $('#built_up_nl').val();
      var land_area = $('#land_area').val();
      var name = $('#name').val();
      var email = $('#email').val();
      var contact = $('#contact').val();
     

      var  submitUrl = '';
      submitUrl = "https://lazacode.org/erp_report_queue/add";
      submitUrl += "?asset_id="+asset;
      submitUrl += "&property_type="+property_type;
      submitUrl += "&sub_type="+sub_type;
      submitUrl += "&block="+block;
      submitUrl += "&level="+level;
      if (property_type == 'l'){
        submitUrl += "&built_up="+built_up;
      }
      else {
        submitUrl += "&built_up="+built_up_nl;
      } 
      submitUrl += "&land_area="+land_area;
      submitUrl += "&name="+name;
      submitUrl += "&email="+email;
      submitUrl += "&contact="+contact;
      submitUrl += "&state="+ select_state;
 

      if (asset==null || asset=="") {  
          alert("Projects / Townships / Street name can't be empty");
          return false;

      } else if ( property_type =="l" &&  sub_type=="0") {   
          alert("Please choose your sub property type");  
          return false;     

      } else if ( property_type =="l" && (built_up==null || built_up==""))   {   
          alert("The built up input can't be empty");  
          return false;  

      } else if (property_type =="r" && (built_up_nl==null || built_up_nl==""))  {   
          alert("The built up input can't be empty");  
          return false;  

      } else if ( property_type =="l" && (land_area == null || land_area == "")) {   
          alert("The land size input can't be empty");  
          return false; 

      } else if (name==null || name=="") {   
          alert("Your name can't be empty");  
          return false; 

      // } else if (email==null || email=="") {   
      //     alert("Your email can't be empty");  
      //     return false; 

      } else if (email==null || email=="") {
          alert("Your email can't be empty");
          return false;

      } else if (validateEmail(email)) {

      } else if (contact==null || contact=="") {
          alert("Your contact number can't be empty");
          return false;

      } else if (validateContact(contact)) {

      // } else if (contact==null || contact=="") {   
      //     alert("Your contact number can't be empty");  
      //     return false;  
      // } 
      } else {
          alert("Please enter a valid email address  (e.g. me@myemail.com)");
          return false; 
      }

      function validateEmail(email) {
          var filter = /^([\w-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
          if (filter.test(email)) {
              return true;
          }
          else {
              return false;
          }
      }

      function validateContact(contact) {
          var filterContact = /\(?([0-9]{3})\)?([ .-]?)([0-9]{3})\2([0-9]{4})/;
          if (filterContact.test(contact)) {
              return true;
          }
          else {
              return false;
          }
      }


      console.log(submitUrl);

      $.getJSON(submitUrl,
        function(data) {
          if (data == 'success'){
            console.log("redirecting..");
            window.location.replace("homeadvisor-success.html");
          }
        }
      );
    });
  })

 

lazacode.org - Malaysia's programming knowledge sharing platform, where everyone can share their finding as reference to others.
...