Every time I try to get data from the directions API, I get No 'Access-Control-Allow-Origin' header is present on the requested resource. I've tried to do the same request to the geocode API and it works. This is the code that I'm using:
var xhr = new XMLHttpRequest();
xhr.open('GET', 'https://maps.googleapis.com/maps/api/directions/json?origin='+encodeURIComponent(data.origin)+'&destination='+encodeURIComponent(data.destination)+'&key='+encodeURIComponent(data.key), true);
xhr.send();
You cannot access to the directions api with ajax request because this api dont support CORS. But, you can use the library to access to the Direction API data.
Here a example taken from Google Maps API Examples: