2017-03-15 65 views
-2

我使用JavaScript進行編程,並且我有API,它給了我一個JSON對象。 這是 - >http://193.70.60.44:3000/geoserver/taxi_server/api/v1.0/taxi對象網址。從JavaScript中的外部url中獲取JSON對象

在這個JSON對象中有JSONOBJECT.position.coordinates [0],它給了我一個位置的經度。

我如何獲得JavaScript中的JSON對象並使用座標?

+1

你的API提供了響應:'{「消息」:「歡迎來到虛無的開始。」} ' –

+1

發佈的URL沒有Access-Control-Allow-Origin標頭,它不是JSONP,因此無法直接在瀏覽器中獲取該JSON – adeneo

回答

0

像這樣使用jQuery(編輯),但在你返回的對象使用適當的屬性:

var myUrl = 'http://siteofjson.org/getjson/'; 
$.getJSON(myUrl, function(JSONOBJECT){ 
    console.log(JSONOBJECT.position.coordinates[0]); 
    var lonLat = new OpenLayers.LonLat(JSONOBJECT.position.coordinates[0].lon,JSONOBJECT.position.coordinates[0].lat) 
     .transform(
      new OpenLayers.Projection("EPSG:4326"), // Transformation aus dem Koordinatensystem WGS 1984 
      map.getProjectionObject() // in das Koordinatensystem 'Spherical Mercator Projection' 
     ); 
});