2
I have an ajax + a txt simple script and can access the txt data, but I want to select the data of an array in ajax, example, search only the name or only 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 it like this:
document.getElementById("myDiv").innerHTML = xmlhttp.responseText;
carro.tipo;
I tried so:
document.getElementById("myDiv").innerHTML = xmlhttp.responseText.carro.tipo;
But it didn’t work. How to access certain data in txt
with ajax
?
Sérgio’s solution solves his problem, but has already considered cleaning a server with a real DBMS?
– user25930
Hi Adailton! If you want you can mark the answer as accepted.
– Sergio