How to implement cross-domain access in tomcat when using only jsp and javascript / ajax / jQuery and not using php?

I am developing a GIS Map Java web application using jsp , javascript / ajax / jQuery, which deploys on a tomcat server . I need to implement cross-domain access here to get a response from google api that returns a response in json format. But since access to the cross domain is not possible with xmlhttp, I cannot get an answer.

I have seen several posts suggesting use proxy.phpon the client side. But I do not use php, and I would like to know if there is a way to implement this using only jsp/javascript. Is there any special configuration in tomcat? Please help.

Here is what I am trying 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 because cross-domain access is not possible. Request for help

Thank you and respectfully Ginger.

+4
source share
1 answer

I solved the problem. The only cross-domain solution (which I think) uses cross-domain proxy access.

Here's how to do it.

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
source

All Articles