I am new to Javascript and Ajax. I was trying to write some code wherein I parse the JSON response of a HTTP request and then access different elements within the response object. But I keep running into 2 different errors. I tried looking through various solutions on stack overflow, but i havent been able to get them to work. Any help would be greatly appreciated.
var xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
var json = JSON.parse(this.responseText);
document.getElementById("lola1").innerHTML = json;
initMap(json);
}
};
I want to pass in only the results array from the response object. However if I do this, The page display is " [object Object] "
When I try to do JSON.parse within the initMap method, then i get a syntax error on JSON.parse
SyntaxError: JSON Parse error: Unexpected identifier "undefined"
if (this.readyState == 4 && this.status == 200) {
var json = this.responseText;
initMap(json);
}
function initMap(response_val) {
// Process input results
document.getElementById("lola1").innerHTML = JSON.parse(response_val)
...
Thanks.