我有以下代码用于从OSM请求 map 数据:
$.ajax({
url:
'https://www.overpass-api.de/api/interpreter?' +
'[out:json][timeout:60];' +
'area["boundary"~"administrative"]["name"~"Berlin"];' +
'node(area)["amenity"~"school"];' +
'out;',
dataType: 'json',
type: 'GET',
async: true,
crossDomain: true
}).done(function() {
console.log( "second success" );
}).fail(function(error) {
console.log(error);
console.log( "error" );
}).always(function() {
console.log( "complete" );
});
当我在Overpass Turbo上测试该请求时,它可以正常运行,但是在JavaScript中执行该请求时,总是会收到错误消息:
"<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head> <meta http-equiv="content-type" content="text/html; charset=utf-8" lang="en"/> <title>OSM3S Response</title></head><body><p>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: Key expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: '!', '~', '=', '!=', or ']' expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: Value expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: ',' or ']' expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: Key expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: '!', '~', '=', '!=', or ']' expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: Value expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: ',' or ']' expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: static error: For the attribute "k" of the element "has-kv" the only allowed values are non-empty strings. </p><p><strong style="color:#FF0000">Error</strong>: line 1: static error: For the attribute "k" of the element "has-kv" the only allowed values are non-empty strings. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: Key expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: '!', '~', '=', '!=', or ']' expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: Value expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: parse error: ',' or ']' expected - '%' found. </p><p><strong style="color:#FF0000">Error</strong>: line 1: static error: For the attribute "k" of the element "has-kv" the only allowed values are non-empty strings. </p></body></html>"
我执行请求的方式一定存在问题,但是我无法弄清楚它可能有什么问题。
如何通过JavaScript获取柏林所有学校的职位?
我也尝试使用
$.getJSON()
,但这对我也不起作用。 最佳答案
您在示例中使用的网址似乎不完整:应显示为... interpreter?data=[out:json]
...。即缺少 data = 部分。
作为引用,您还可以将查询放入立交涡轮增压器中,只需直接从Overpass API中单击“导出”->“原始数据”即可获得有效的URL。也许先用wget尝试一下,如果可以,将URL放入Javascript代码中。
也许您还想研究一下基于POST的Overpass Turbo如何调用Overpass API:有关详细信息,请参见https://github.com/tyrasd/overpass-turbo/blob/master/js/overpass.js#L581。