从 JavaScript 中的 URL 调用 JSON 文件

Calling JSON file from URL in JavaScript

本文关键字:JSON 文件 调用 URL JavaScript 中的      更新时间:2023-09-26

我正在尝试从网页中读取JSON文件,并在OpenLayers的地图上显示文件中包含的路线。我找到了另一个与我类似的例子,如何使用JavaScript从URL获取JSON?,但我无法让它工作。

例如,我创建了包含以下内容的 URL 字符串:

http://router.project-osrm.org/viaroute?rebuild=1&loc=43.46711564169348,-3.880102031707764&loc=43.4669443349282,-3.862788677215576&output=json

此网页应返回一个 JSON 文件,其中包含我到达终点必须遵循的所有点。我知道这有效,因为我尝试了这个例子:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
    <title>Open Space Web-Map builder Code</title>
</head>
<body>
    <div class="header-content">
    [
    <a class="result-link" onClick="document.location.href='http://router.project-    osrm.org/viaroute?  rebuild=1&amp;loc=43.46711564169348,-3.880102031707764&amp;loc=43.4669443349282,-3.862788677215576&amp;output=json';">Generar ruta</a>
    ]
    </div>
</body>
</html>​

它返回一个 JSON 文件,如下所示。但是如果我尝试使用我的页面,它不起作用。我有这个函数来读取 JSON 文件:

function pintarRutaCamion() {
    capaRuta = new OpenLayers.Layer.Vector("capaRuta");
    var style_green = {
        strokeColor: "#00FF00",
        strokeOpacity: 1,
        strokeWidth: 6
    };
    var pointRuta = [];
    alert(rutaCompleta); //show the complete url
    JQ.getJSON(rutaCompleta, function(puntosRuta) {
        alert(puntosRuta.route_geometry.length); //show size of returned json file
        for (i = 0; i < puntosRuta.route_geometry.length; i++) {
            coordenadas = new OpenLayers.LonLat(puntosRuta.route_geometry[i][1], puntosRuta.route_geometry[i][0]).transform(new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject());
            pointruta.push(coordenadas);
        }
    });
    //create a polyline feature from the array of points 
    var lineString = new OpenLayers.Geometry.LineString(pointRuta);
    var lineFeature = new OpenLayers.Feature.Vector(lineString, null, style_green);
    capaRuta.addFeatures([lineFeature]);
    //add it to the map 
    map.addLayer(capaRuta);
}​

JSON 文件应该是这样的:

{"version": 0.3,
 "status": 0,
 "status_message": "Found route between points",
 "route_geometry": [[43.46716, -3.87987],[43.46668, -3.87963],[43.46706, -3.87761],[43.46593, -3.87740],[43.46571, -3.87552],[43.46559, -3.87515],[43.46553, -3.87512],[43.46549, -3.87504],[43.46548, -3.87496],[43.46550, -3.87487],[43.46554, -3.87482],[43.46558, -3.87433],[43.46533, -3.87210],[43.46535, -3.87185],[43.46546, -3.87128],[43.46592, -3.86911],[43.46598, -3.86859],[43.46593, -3.86824],[43.46589, -3.86818],[43.46587, -3.86808],[43.46588, -3.86800],[43.46581, -3.86780],[43.46560, -3.86761],[43.46545, -3.86756],[43.46526, -3.86756],[43.46517, -3.86760],[43.46511, -3.86760],[43.46502, -3.86753],[43.46498, -3.86743],[43.46497, -3.86734],[43.46499, -3.86718],[43.46510, -3.86711],[43.46521, -3.86696],[43.46530, -3.86675],[43.46547, -3.86606],[43.46569, -3.86504],[43.46639, -3.86166],[43.46716, -3.86194],[43.46698, -3.86278]],
 "route_instructions": [["10","",56,0,155,"56m","SE",203.5],["7","",167,1,242,"167m","E",281.06],["3","Calle Polvorín",126,2,182,"126m","S",189.18],["7","CA-231",185,3,131,"185m","E",262.42],["11-2","CA-231",536,10,350,"536m","E",277.7],["11-1","CA-231",82,20,52,"82m","E",250.51],["11-2","Calle del Somo",36,31,19,"36m","NE",310.15],["1","Calle de El Somo",426,33,236,"426m","E",285.81],["7","Calle de Antonio Nebrija",88,36,127,"88m","N",17.56],["7","Calle de Manuel Cacicedo",70,37,76,"70m","W",103.84],["15","",0,38,0,"","N",0.0]],
 "route_summary": {"total_distance": 1890,
                   "total_time": 179,
                   "start_point": "",
                   "end_point": "Calle de Manuel Cacicedo"},
 "via_points": [],
 "hint_data": {"checksum": -1013584035,
               "locations": ["xqyjHgAAAACbAAAAzwAAABj5Tb5MZ9s_XFNCAG0U-v9", "WVr_FtzAKgAzAQAAaAAAAK5H_5Np-ec_SlNCABob-v9"]},
 "transactionId": "OSRM Routing Engine JSON Descriptor (v0.3)"}

但不可能进入该功能。我不知道会发生什么。我尝试将document.location.href=编写为 URL 字符串中的另一个示例,但这也失败了。谁能建议为什么这不起作用?

你的网址应该是这样的,

http://router.project-osrm.org/viaroute?rebuild=1&loc=43.46711564169348,-3.880102031707764&loc=43.4669443349282,-3.862788677215576&output=json

而且似乎不支持 jsonp 回调,因此由于跨源资源共享,您无法直接使用 $.getJSON 请求。

您应该使用代理来避免此问题,例如您可以使用 YQL

下面是您的网址示例