2013-03-20 59 views
2

我想讀取和張貼在jqGrid中的一組地震GeoJSON數據從USGS存儲庫中提取。 該請求被接受,但在可能符合標題元數據時顯示「Uncaught SyntaxError:Unexpected token」。jqGrid from USGS geojson data

$(function() { 
    'use strict'; 
    $.extend($.jgrid.search, {multipleSearch: true, multipleGroup: true, overlay: 0}); 
    $('#grid').jqGrid({ 
     url: 'http://earthquake.usgs.gov/earthquakes/feed/geojson/2.5/week?callback=?', 
     datatype: 'json', 
     colModel: [ 
      {name: 'mag', label: 'MAGNITUDO', width: 150, jsonmap: 'properties.mag', sorttype: 'number', 
formatter: 'number', formatoptions: {decimalPlaces: 2}}, 
      {name: 'place', label: 'LOCALITA', width: 150, jsonmap: 'properties.place'}, 
      {name: 'url', label: 'URL', width: 150, jsonmap: 'properties.url'} 
     ], 
     toppager: true, 
     gridview: true, 
     rowList: [10, 20, 50, 10000], 
     rowNum: 10, 
     jsonReader: { 
      root: 'features', 
      repeatitems: false 
     }, 
     loadonce: true, 
     ignoreCase: true, 
     height: 'auto' 
    }).jqGrid('navGrid', '#grid_toppager', {add: false, edit: false, del: false}) 
     .jqGrid('filterToolbar', {stringResult: true, defaultSearch: 'cn', searchOnEnter: false}); 
    $("#grid_toppager option[value=10000]").text('All'); 
}); 

你有什麼解決方法嗎? 在此先感謝。

+0

您是否測試過JSON有效? – Mark 2013-03-20 10:37:51

+0

雖然GeoJSON是一個地理空間數據,但指向根的是「特徵」。我認爲沒有問題。也許我錯了? – user2190221 2013-03-20 11:28:30

回答

2

我看着geojson的文檔,我想我找到了問題的原因。 GeoJSON(P)似乎使用eqfeed_callback作爲回調名稱(請參閱here)。所以,我定你使用以下jqGrid的一些選項:

url: 'http://earthquake.usgs.gov/earthquakes/feed/geojsonp/2.5/week', 
datatype: 'jsonp', 
postData: '', 
ajaxGridOptions: { jsonp: false, jsonpCallback: 'eqfeed_callback', cache: true}, 

The modified demo作品現在並顯示如下圖所示

結果

enter image description here

UPDATE:The modified demo使用GeoJSON的新網址和新版本(4.14.1)free jqGrid

+0

偉大的奧列格,現在我已經更好地理解了「回調」的jQuery中的功能。謝謝 – user2190221 2013-03-20 11:29:10

+0

@ user2190221:不客氣!如果問題現在得到解決,您應該[「接受」](http://meta.stackexchange.com/a/5235/147495)答案。順便說一句[這裏](http://stackoverflow.com/a/9588051/315935)和[這裏](http://stackoverflow.com/a/4326986/315935)你可以找到JQGrid中使用JSONP的另一個例子。 – Oleg 2013-03-20 11:33:29