1

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.

4

2 回答 2

3

解决它的最简单方法是告诉服务您想要jsonp,然后使用服务提供的回调。

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);
于 2013-10-17T21:54:01.337 回答
3

在http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojsonp使用他们的 JSONP 服务

$.ajax({
    url: 'http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojsonp',
    dataType: 'jsonp',
    jsonp: false,
    jsonpCallback: 'eqfeed_callback'
}).done(function(data) {
    console.log(data);
});
于 2013-10-17T21:56:48.670 回答