fares alkhwaja fares alkhwaja - 3 months ago 10
jQuery Question

get data after api call using ajax

I need to get the data after ajax function request succeed , the data i need to return :

{
"total_count": 391,
"incomplete_results": false,
"items": [
{
"login": "pascalfares",
"id": 1664478,
"avatar_url": "https://avatars.githubusercontent.com/u/1664478?v=3",
}
]


my code is:

$.ajax({

url: 'https://api.github.com/search/users?q=' + $('#search').val(),
dataType: 'jsonp',
type: 'GET',
success: function(data) {
// foreach to get the data
}
});

Answer

you can use console.log(data) in success function and view result in browser console .
this API returned two object : data and meta.
try this

success: function(data) {
      for(var i = 0; i< data.data.items.length; i++){
         var login =data.data.items[i].login;
          var id = data.data.items[i].id;
          console.log("ID = " +id + " Login = " + login);
      }
}

  $(document).ready(function(){
    $('#btn').click(function(){

      $.ajax({
        url: 'https://api.github.com/search/users?q=' + $('#search').val(),
        dataType: 'jsonp',
        type: 'GET',
        success:function(data) {
       for(var i = 0; i< data.data.items.length; i++){
         var login = data.data.items[i].login;
         var id=data.data.items[i].id;
         $('#list').append("<li> Login :" +login+" | Id:"+id+"</li>");
      }
    }
  });
    });

  });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input id="#search" />
  <button id="btn" >clck</button> 
<br/><br/>
<ul id="list">
  </ul>



console.log logs a message to a debugging console.