简体   繁体   English

getJSON意外令牌错误

[英]getJSON unexpected token error

I'm trying to get earthquake data from USGS and I keep getting the error: 我正在尝试从USGS获取地震数据,但不断收到错误消息:

Uncaught SyntaxError: Unexpected token : 未捕获到的SyntaxError:意外令牌:

I tried $.ajax with jsonp format and I keep getting the same issue. 我尝试使用jsonp格式的$ .ajax,但仍然遇到相同的问题。 I tried without callback at the end of my url as well, in that case I get the error: 我也尝试在URL末尾没有回调的情况下出现这种情况:

MLHttpRequest cannot load http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson . MLHttpRequest无法加载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. Access-Control-Allow-Origin不允许使用来源http://people.oregonstate.edu

$.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. 有人可以帮助我如何获取数据,或者如果不是这种方式,则可能不是jQuery。

the easiest way to get around it would be to tell the service you want jsonp, then use the callback provided by the service. 解决该问题的最简单方法是告诉服务您想要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);

Use their JSONP service at http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojsonp 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);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM