Search code examples
javascriptjqueryajaxjsonjsonp

getJSON unexpected token error


I'm trying to get earthquake data from USGS and I keep getting the error:

Uncaught SyntaxError: Unexpected token :

I tried $.ajax with jsonp format and I keep getting the same issue. I tried without callback at the end of my url as well, in that case I get the error:

MLHttpRequest cannot load http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson. Origin http://people.oregonstate.edu is not allowed by Access-Control-Allow-Origin.

$.getJSON(
   "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson&callback=?",
    function(data) {
       console.log(data);
    }
 );

can someone help me out how to get the data or perhaps something other than jQuery if it is not possible this way.


Solution

  • the easiest way to get around it would be to tell the service you want jsonp, then use the callback provided by the service.

    window.eqfeed_callback = function(data){
        console.log(data);
    };
    //$.getScript("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojsonp");
    var s = document.createElement("script");
    s.src = "http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojsonp";
    document.getElementsByTagName("head")[0].appendChild(s);