0

I have try to get the elevation of site based on latitude and longitude. When i am using the ajax call i am getting error. I have tried both HTTP and HTTPS getting same error message is

"No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access."

But the same URL it gives result in browser (https://maps.googleapis.com/maps/api/elevation/json?locations=36.7391536,-110.9847034).

I have added some header information like ('Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': true,'Access-Control-Allow-Methods': 'GET,PUT,POST,DELETE'). dataType : 'json' / 'jsonp' / 'application/json' and crossDomain: true, But it doesn't worked.

The ajax call is working for geocode URL but its not working for elevation URL

siteElevationlatLong: function (lat, long) {              
    var deferred = $q.defer();
    $.ajax({
        url: 'https://maps.googleapis.com/maps/api/elevation/json',              
        data: {
            sensor: false,
            locations: lat +',' + long,
        },
        dataType: 'json',
        success: function (data) {                      
            if (data.status == "OK" || data.status == "ZERO_RESULTS") {
                deferred.resolve(data.results);
            } else {
                deferred.resolve([]);
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {                      
            deferred.reject(jqXHR.responseText);
        }
    });
    return deferred.promise;
}
Dinesh J
  • 113
  • 11
  • Ya i have tried Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Credentials': true these two then also i am not getting result – Dinesh J Dec 12 '17 at 03:15
  • The headers have to be set on the response from the server, i.e. by Google. – david25272 Dec 12 '17 at 03:19
  • Then how can i achieve this one – Dinesh J Dec 12 '17 at 03:21
  • I think you'll probably need to use a different API. You're trying to use an API designed for app developers. See https://developers.google.com/maps/documentation/javascript/elevation for a browser version. – david25272 Dec 12 '17 at 03:34
  • Bro i am not showing the any Map here just i need a elevation based on lat and long – Dinesh J Dec 12 '17 at 03:59

1 Answers1

0

can you try it by changing datatype from " dataType: 'jsonp'" or "datatype: 'application/json'"

Arun
  • 450
  • 3
  • 8
  • Ya bro i have tried with JSONP but it doesn't worked out – Dinesh J Dec 12 '17 at 03:06
  • While i am using JSONP i getting error like parseerror and in console log "Uncaught SyntaxError: Unexpected token :" – Dinesh J Dec 12 '17 at 03:14
  • mention the type of call 'post' and revert changes from jsonp to json – Arun Dec 12 '17 at 03:15
  • ok just a min i will check and let u know – Dinesh J Dec 12 '17 at 03:17
  • Ya bro its also not working. getting same error msg while trying in 'json' (No 'Access-Control-Allow-Origin' header is present on the requested resource). and in 'jsonp' "Uncaught SyntaxError: Unexpected token :". – Dinesh J Dec 12 '17 at 03:19
  • add another property crossDomain: true – Arun Dec 12 '17 at 03:25
  • Same error message bro – Dinesh J Dec 12 '17 at 03:29
  • -disable-web-security -user-data-dir="D:/Chrome in chorme property. right click on chrome -> shortcut->target add the above line. For Example ""C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" -disable-web-secur ity -user-data-dir="D:/Chrome"" – Arun Dec 12 '17 at 03:34
  • or try with adding extension to enable CORRs – Arun Dec 12 '17 at 03:36
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160987/discussion-between-arun-and-dinesh-j). – Arun Dec 12 '17 at 03:42