I have a simple ajax + txt script and I can access the txt data, but I want to select the data of an array in ajax, for example, search only the name or just the age. See the code;
<!DOCTYPE html>
<html>
<head>
<script>
function loadXMLDoc(){
var xmlhttp;
if (window.XMLHttpRequest){// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","ajaxnotxt.txt",false);
xmlhttp.send();
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
</script>
</head>
<body>
<div id="myDiv">
<h2>Let AJAX change this text</h2>
</div>
<button type="button" onclick="loadXMLDoc()">Change Content</button>
</body>
</html>
and has a txt called ajaxnotxt.txt
that has a variable like this:
var carro = {tipo:"fiat", modelo:"500", cor:"branco"};
I want to access this way:
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
carro.tipo;
I tried it like this:
document.getElementById("myDiv").innerHTML = xmlhttp.responseText.carro.tipo;
But it did not work. How to access given data in txt
with ajax
?