Problem accessing JSON output from Directions Web Services API

1,635 views
Skip to first unread message

sandy037

unread,
Mar 1, 2011, 7:55:52 PM3/1/11
to Google Maps JavaScript API v3
Hi all,
I am trying to get the JSON output of directions between two points
using the Directions API Web services. I am using an XmlHttpRequest
(GET Method) in Javascript to get the results but gives me an error
"XMLHttpRequest cannot load
http://maps.googleapis.com/maps/api/directions/json?origin=43.0480556....
Origin http://localhost:51724 is not allowed by Access-Control-Allow-
Origin."
If I paste the URL in browser and access it, it gives me the result
properly. I am using IIS 7 as the server and I included the custom
header "Access-Control-Allow-Origin" as ' * ' to allow CORS (Cross
Origin Request Security), but still could not access the result. I
donot have a working link of my code sample, so I am pasting the
source below
var url = "http://maps.googleapis.com/maps/api/directions/json?
origin=43.0480556,-76.1477778&destination=41.882853,-87.642059&avoid=tolls&
sensor=false";
var request = new XMLHttpRequest();
request.open("GET", url, true);
request.setRequestHeader("Access-Control-Allow-Origin",
"*");
request.onreadystatechange = function () {
if (request.readyState == 4) {
// === Parse the JSON document ===
var json = request.responseText;
alert(json);
}
};
request.send(null);
Please help me to sort out this.
Thanks & Regards,
SDee.

geoco...@gmail.com

unread,
Mar 1, 2011, 8:09:15 PM3/1/11
to Google Maps JavaScript API v3
On Mar 1, 4:55 pm, sandy037 <sandyv...@gmail.com> wrote:
> Hi all,
> I am trying to get the JSON output of directions between two points
> using the Directions API Web services. I am using an XmlHttpRequest
> (GET Method) in Javascript to get the results but gives me an error
> "XMLHttpRequest cannot loadhttp://maps.googleapis.com/maps/api/directions/json?origin=43.0480556....
> Originhttp://localhost:51724is not allowed by Access-Control-Allow-
> Origin."

Why? That web service is intended for servers, there is a native
directions service as part of the API.

Perhaps if you send it from public web server it would work, I could
see why localhost might not be allowed for the WebService (it is
allowed for testing from the native API).


> If I paste the URL in browser and access it, it gives me the result
> properly. I am using IIS 7 as the server and I included the custom
> header "Access-Control-Allow-Origin" as ' * ' to allow CORS (Cross
> Origin Request Security), but still could not access the result. I
> donot have a working link of my code sample, so I am pasting the
> source below

No thanks.

-- Larry

Michael Geary

unread,
Mar 1, 2011, 8:26:14 PM3/1/11
to google-map...@googlegroups.com
On Tue, Mar 1, 2011 at 4:55 PM, sandy037 <sand...@gmail.com> wrote:
Hi all,
I am trying to get the JSON output of directions between two points
using the Directions API Web services. I am using an XmlHttpRequest
(GET Method) in Javascript to get the results but gives me an error
"XMLHttpRequest cannot load
http://maps.googleapis.com/maps/api/directions/json?origin=43.0480556....
Origin http://localhost:51724 is not allowed by Access-Control-Allow-
Origin."

You can't use XMLHttpRequest to make a request to another domain. The browser doesn't allow that.

You can access that web service from your server, or you could use DirectionsService() in the V3 Maps API.

-Mike

sandy037

unread,
Mar 2, 2011, 10:05:49 AM3/2/11
to Google Maps JavaScript API v3
I know that DirectionsService() is there, but I want to know if there
is some way to access the Web Services API and get the result.

geoco...@gmail.com

unread,
Mar 2, 2011, 10:12:41 AM3/2/11
to Google Maps JavaScript API v3
On Mar 2, 7:05 am, sandy037 <sandyv...@gmail.com> wrote:
> I know that DirectionsService() is there, but I want to know if there
> is some way to access the Web Services API and get the result.

From your server there is. Why do you need to access it from the
client?

-- Larry

Andrew Leach

unread,
Mar 2, 2011, 10:18:55 AM3/2/11
to google-map...@googlegroups.com
On 2 March 2011 15:05, sandy037 <sand...@gmail.com> wrote:
> I know that DirectionsService() is there, but I want to know if there
> is some way to access the Web Services API and get the result.

Not from the client, because the browser stops it happening. The
Javascript specification prevents cross-domain access with
XMLHttpRequest. If you're using directions client-side, you should use
the client-side tools provided.

The standard workaround for cross-domain requests is to implement a
proxy on your own domain.

Note: even though you have used Access-Control-Allow-Origin, it is set
on *your* server, not Google's, only IE8/9 will recognise it; and it
requires XDomainRequest, not XMLHttpRequest. Google's servers may
respond to XDomainRequests from arbitrary clients, but you can't force
that.

http://msdn.microsoft.com/en-us/library/dd573303%28v=vs.85%29.aspx

sandy037

unread,
Mar 2, 2011, 11:46:48 AM3/2/11
to Google Maps JavaScript API v3
Or can use a dynamic script tag and write a callback function to get
the results in JSON format?? Does google directions web services
allows us to specify a callback function?

sandy037

unread,
Mar 2, 2011, 12:06:12 PM3/2/11
to Google Maps JavaScript API v3

I want to make a mobile application with maps. Since V3 may take more
time to load, I am just wondering if we can use the web services.

On Mar 2, 10:12 am, "geocode...@gmail.com" <geocode...@gmail.com>
wrote:

Andrew Leach

unread,
Mar 2, 2011, 12:20:52 PM3/2/11
to google-map...@googlegroups.com

Doesn't look like it. Even if it works, it couldn't be relied upon
because it's not documented.

http://code.google.com/apis/maps/documentation/directions/#RequestParameters

Andrew Leach

unread,
Mar 2, 2011, 12:23:03 PM3/2/11
to google-map...@googlegroups.com
On 2 March 2011 17:06, sandy037 <sand...@gmail.com> wrote:
>
> I want to make a mobile application with maps. Since V3 may take more
> time to load, I am just wondering if we can use the web services.

May take more time to load than what?

Basically, you have two choices: either use the client tools, or
implement a proxy (which will come with its own hazards, like using
the server's ip address).

sandy037

unread,
Mar 2, 2011, 12:29:22 PM3/2/11
to Google Maps JavaScript API v3
Thanks for all the updates. I will go with V3 API for my application.

bratliff

unread,
Mar 2, 2011, 12:50:14 PM3/2/11
to Google Maps JavaScript API v3
It may not be legal:

http://www.polylib.us/backdoor

Check with Google. I asked for an opinion several months ago. I
never received it.
Reply all
Reply to author
Forward
0 new messages