Accessing .txt file data by Ajax

2

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 ?

    
asked by anonymous 22.06.2015 / 04:02

1 answer

3

You have 2 ways to do this and right now you are trying to get an intermediate version that is wrong.

If you have in your file .txt JavaScript code as shown in the question:

var carro = {tipo:"fiat", modelo:"500", cor:"branco"};

Then you can load this script not by ajax but by append script directly on the page. Then the variable carro will be global and you can access as you want.

In this case the code should be:

var script = document.createElement('script');
script.type = 'text/javascript';
script.src = 'ajaxnotxt.txt';
script.onload  = function() {
    document.getElementById("myDiv").innerHTML = carro.tipo;
}
document.body.appendChild(script);

If you want to use AJAX , then you should change your file .txt to have only one JSON string that you can use later in JavaScript.

So in the file you would have {"tipo":"fiat", "modelo":"500", "cor":"branco"} and AJAX:

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.onreadystatechange = function () {
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
            var carro = JSON.parse(xmlhttp.responseText);
            document.getElementById("myDiv").innerHTML = carro.tipo;
        }
    }
    xmlhttp.open("GET", "ajaxnotxt.txt", false);
    xmlhttp.send();
}

This way is best and does not export variables into global space ...

    
22.06.2015 / 09:32