0

I created a very simple rest API and following the sample on this other post : HERE I made an ajax call to my API but for some reason I get undefined as a response. I think I'm doing something wrong in the call.

Here is the call:

$( document ).ready(function() {
    FetchData();
});
function FetchData() {
    $.ajax({
        async: false,
        type: "GET",
        url: "http://austinwjones.com/radio/gunsmoke/read.php",
        dataType: "json",
        success: function(data, textStatus, jqXHR) {
            $.each(data, function(i, object) {
                console.log(data.records.epi);
            });
        },
        error: function() {

        }
    });
}

By directly going to my API Here you can see there is a result. I suspect my issue is with this part?

 success: function(data, textStatus, jqXHR) {
            $.each(data, function(i, object) {
                console.log(data.records.epi);
            });

Any help is appreciated.

Austin Jones
  • 709
  • 1
  • 14
  • 29

2 Answers2

1

Just another solution, with baked Promise, simple jQuery/JSON solution.

function getJSON() {
  return new Promise((resolve, reject) => {
    jQuery
    .getJSON('http://austinwjones.com/radio/gunsmoke/read.php')
    .then((data) => {
      jQuery.each(data.records, (i, record) => {
        console.log(record);
        return resolve(record);
      })
    });
  });
}

getJSON()
.then((records) => {
  console.log(records);
})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

This way, you can get everything and display it, once it's been fetched.

Anuga
  • 2,619
  • 1
  • 18
  • 27
0

you should rather change it to:

for (var i = 0; i < data.records.length; i++) {
  console.log( data.records[i].epi );
}

since records is an array.

Alex
  • 9,911
  • 5
  • 33
  • 52