I'm trying to access a geological website data using their API and Ajax to retrieve from it.
var location;
var titleName;
$(document).ready(function() {
  $('#earthquakes').click(function() {
    function getQuakes() {
      $.ajax({
        url: "http://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime=2014-01-01&endtime=2014-01-02",
        function(data) {
          $.each(data.features, function(key, val) {
            var coord = val.geometry.coordinates;
            location = {
              lat: coord[0],
              lng: coord[1]
            };
            titleName = val.properties.title;
          });
        }
      });
    }
    console.log(location);
    console.log(titleName);
  });
});<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button type="button" id="earthquakes">Click me</button>In the result, when I click "earthquakes" button, it logs two lines:
"Location {hash: "", search: "", pathname: "/", port: "8888", hostname: "localhost"…}"
and
undefined
I seem to be targeting the right parameters with my function, but still, it doesn't get the data I need. Just for the reference here is their documentation: http://earthquake.usgs.gov/fdsnws/event/1/ But I believe the problem might be something else and I might be doing it wrong with variables and the way they are used.
 
     
    