在46字符串中如果我手动输入点的名称而不是变量res
[点的名称]它可以工作。但如果没有,那就行不通了。
它的工作原理如下:
我哪里出错?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript" src="http://ecn.dev.virtualearth.net/mapcontrol/mapcontrol.ashx?v=7.0"></script>
<script type="text/javascript">
var map = null;
function GetMap() {
// Initialize the map
map = new Microsoft.Maps.Map(document.getElementById("mapDiv"), {
credentials: "An-KyHsPpYClz9oHXMZ6X3SW6I44Ei2DTTrUS_QlXUW43qqsvOAQrT9ekVEIBT9u"
});
Microsoft.Maps.Events.addHandler(map, 'click', LatLong);
}
function LatLong(e) {
if (e.targetType == "map") {
var point = new Microsoft.Maps.Point(e.getX(), e.getY());
var loc = e.target.tryPixelToLocation(point);
lola = "lo,loe".replace('lo', loc.latitude).replace('loe', loc.longitude);
}
MakeGeocodeRequest('An-KyHsPpYClz9oHXMZ6X3SW6I44Ei2DTTrUS_QlXUW43qqsvOAQrT9ekVEIBT9u');
}
function ClickGeocode(credentials) {
map.getCredentials(MakeGeocodeRequest);
}
function MakeGeocodeRequest(credentials) {
var geocodeRequest = "http://dev.virtualearth.net/REST/v1/Locations/" + lola + "/?output=json&jsonp=GeocodeCallback&key=" + credentials;
CallRestService(geocodeRequest);
}
function GeocodeCallback(result) {
res = (result.resourceSets[0].resources[0].name);
console.log(res);
$(document).ready(function () {
$.ajax({
type: "GET",
url: "http://en.wikipedia.org/w/api.php?action=opensearch&limit=1&format=xml&search="+res,
dataType: "xml",
success: xmlParser
});
});
}
function xmlParser(xml) {
var str = jQuery(xml).find("Url").text()
alert(str);
document.getElementById("iframe").src=str.replace('en.', 'm.');
}
function CallRestService(request) {
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.setAttribute("src", request);
document.body.appendChild(script);
}
</script>
</head>
<body onload="GetMap();">
<iframe align="right" height="500px" id="iframe" src="" width="48%"></iframe>
<div id='mapDiv' style="position:relative; width:48%; height:500px;"></div>
</body>
</html>
答案 0 :(得分:0)
所以我们基本上有这个:
function GeocodeCallback(result) {
res = "Wróblew, Woj. Lodzkie";
$(document).ready(function () {
$.ajax({
type: "GET",
url: "http://en.wikipedia.org/w/api.php?action=opensearch&limit=1&format=xml&search="+res,
dataType: "xml",
success: xmlParser
});
});
}
我可以发现的主要问题:
您依靠全局res
变量来为您的AJAX调用中的url
提供信息。如果浏览器尝试同时执行两个AJAX请求,则很容易失败。您错过了var
关键字吗?
您无法在URL中注入随机字符串。在vanilla JavaScript中你使用encodeURIComponent()但我确信jQuery允许简单地传递一个对象。
它可能与您的问题无关(无法从您那里获得确切的错误消息),但这是我最好的猜测。