我正在使用jsp,javascript/ajax/jQuery开发一个GIS应用程序,该应用程序部署在tomcat服务器中。我需要在这里实现跨域访问,以便从google 获得响应,后者以json格式返回响应。但是,由于跨域访问xmlhttp是不可能的,我无法得到响应。
我见过一些建议在客户端使用proxy.php的帖子。但是我不使用php,我想知道是否有单独使用jsp/javascript来实现这个功能。在tomcat中有什么特殊的配置吗?帮帮忙吧。
以下是我要做的事:
var url = "http://maps.googleapis.com/maps/api/directions/json?origin=26.849307092121,75.781290279188&destination=26.932491611988,75.805420139913&alternatives=true&sensor=false";
xmlhttp.open("GET",url,false);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
xmlhttp.send();
function AJAX_addShortestRoute() {
// if(xmlhttp.readyState == 4) {
var response=xmlhttp.responseText;
// document.write(response);
alert(response);
}`但是请求从未被处理过,因为跨域访问是不可能的。请帮帮忙
谢谢并向金格问好。
发布于 2014-04-08 12:05:34
我解决了这个问题。跨域(我认为)的唯一解决方案是通过代理服务器使用跨域访问。
这是如何做到的。
var mapsUrl = 'http://maps.googleapis.com/maps/api/directions/json?origin='+source_y+','+source_x+'&destination='+dest_y+','+dest_x+'&alternatives=true&sensor=true';
var encodedUrl = encodeURIComponent(mapsUrl);
var proxyUrl = 'http://jsonp.guffa.com/Proxy.ashx?url=' + encodedUrl;
$.ajax({
url: proxyUrl,
dataType: 'jsonp',
cache: false,
success: function (result) {
//Your code goes here
}
}); https://stackoverflow.com/questions/20416238
复制相似问题