如何在仅使用jsp和javascript / ajax / jQuery而不使用php时在tomcat中实现跨域访问?

时间:2022-08-23 10:20:05

I am developing a GIS Map Java web application using jsp, javascript/ajax/jQuery which is deployed in tomcat server. I need to implement cross-domain access here to get response from google api which return response in json format. But since cross domain access is not possible with xmlhttp, I cant get a response.

我正在使用部署在tomcat服务器中的jsp,javascript / ajax / jQuery开发GIS Map Java Web应用程序。我需要在这里实现跨域访问以获取来自google api的响应,该响应以json格式返回响应。但由于xmlhttp无法进行跨域访问,因此我无法获得响应。

I have seen some posts suggesting the use of proxy.php in client side. But I am not using php and I would like to know if there is anyway to implement this using jsp/javascript alone. Is there any special configuration to be set in tomcat?? Kindly help.

我看到一些帖子暗示在客户端使用proxy.php。但我不使用PHP,我想知道是否有任何方法只使用jsp / javascript实现这一点。是否有任何特殊配置要在tomcat中设置?请帮助。

Here is what i try to do:

这是我尝试做的:

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);
            }`

But the request is never processed, since cross-domain access is not possible. Kindly help

但是从不处理请求,因为跨域访问是不可能的。请帮助

Thanks and Regards Ginger.

谢谢,关心姜。

1 个解决方案

#1


1  

I resolved the issue. The only solution for cross domain (which I think) is use cross domain access through proxy server..

我解决了这个问题。跨域的唯一解决方案(我认为)是通过代理服务器使用跨域访问。

Here is how it is done.

这是如何完成的。

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
    }
});   

#1


1  

I resolved the issue. The only solution for cross domain (which I think) is use cross domain access through proxy server..

我解决了这个问题。跨域的唯一解决方案(我认为)是通过代理服务器使用跨域访问。

Here is how it is done.

这是如何完成的。

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
    }
});