Im trying to geocode in my javascript code against Geocoding API. I get url defined
var url = "http://http://maps.googleapis.com/maps/api/geocode/json?address=New York, Manhattan&sensor=false"
from which I need to retrive a JSON object. First approach I used was according to Wikipedia using XmlHttpRequest like this
var my_JSON_object = {};
var http_request = new XMLHttpRequest();
http_request.open("GET", url, true);
http_request.onreadystatechange = function () {
var done = 4, ok = 200;
if (http_request.readyState == done && http_request.status == ok) {
my_JSON_object = JSON.parse(http_request.responseText);
}
};
I run the code and get such output in debugger after open() function was triggered:
http_request: XMLHttpRequest
onabort: null
onerror: null
onload: null
onloadend: null
onloadstart: null
onprogress: null
onreadystatechange: function () {
readyState: 1
response: ""
responseText: ""
responseType: ""
responseXML: null
status: [Exception: DOMException]
statusText: [Exception: DOMException]
upload: XMLHttpRequestUpload
withCredentials: false
__proto__: XMLHttpRequest
which means that nothing really useful obviously happened. My second approach was due to json.org, Ive tried to make a get call with JSONRequest
var requestNumber = JSONRequest.get(
url,
function (requestNumber, value, exception) {
if (value) {
document.write(value);
} else {
document.write(exception);
}
}
Debugger told me that JSONRequest is not defined! Due to json.org its native JS object so whats the problem? Just to notice, i use properly gained API key from google in the script nearby.
Explanation why my solution failed and any suggestion how to comply with this task would be much appreciated!