2016-03-08 86 views
-1

我真的不明白我的問題: 我有geojson文件,我需要獲取內容並返回一個有效的變量。獲取文件內容到json/geojson

我的測試:

//this works 
var obj_valid = {"type": "FeatureCollection","crs": { "type": "name", "properties": { "name": "urn:ogc:def:crs:OGC:1.3:CRS84" } },"features": [{ "type": "Feature", "properties": { "id": "14001", "nom": "val1" }, "geometry": { "type": "MultiPolygon", "coordinates": [ [ [ [ 0.301, 49.371 ], xxx , [ 0.301, 49.371 ] ] ] ] } }]}; 

//doesn't work 
var obj_fail = $.getJSON("geojson/com/14001.json"); 

我想回到什麼是我的第一個變種「obj_valid」相同的內容。我試過$ .getJSON,$阿賈克斯,$ .getScript但沒有成功,輸出會有所不同:

enter image description here

什麼是我的錯?

預先感謝您的幫助,

回答

1

它不應該是這樣的

var obj_fail = {}; 
$.getJSON("geojson/com/14001.json") 
.done(function(data){ 
    obj_fail = data; 
}); 

希望這有助於!

3

$.getJSON是一個ajax請求,是異步的。它不返回數據,它返回一個承諾。

需要消耗一個成功的響應數據或進行回調

$.getJSON("geojson/com/14001.json", function(responseData){ 
    // do something with responseData 
}); 

// OR 
$.getJSON("geojson/com/14001.json").done(function(responseData){ 
    // do something with responseData 
}); 

$.getJSON docs