Upload external content with AJAX to global Javascript variable

Asked

Viewed 200 times

1

I’m uploading some data from an external file (.txt) local using AJAX, I would like to know how I can store this data in a global variable for data manipulation, because I need to separate some information and allocate it in tables. NOTE: this page will be executed locally.

Below is the code I’m using.

<html>
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

      <script type="text/javascript">                               
        var temp;
        temp = 100;
        var intervalo = window.setInterval(altura, temp);
        function altura()
          {
            var divh = document.getElementById("DataText").offsetHeight; 
            window.scrollBy(0,divh)                                        
          };

        $(function()
          {
            setTime();
            function setTime()
            {
              $('#DataText').load("https://textuploader.com/151x2");
              setTimeout(setTime, temp);                         
            }
          });                
      </script>
</head>
<body>
    <table border="1">
        <tr>
        <th>Cabeçalho</th>
        <th>Outro Cabeçalho</th>
        <th>Outro Cabeçalho</th>
        </tr>
        <tr>
        <td>linha 1, célula 1</td>
        <td>linha 1, célula 2</td>
        </tr>
        <tr>
        <td>linha 2, célula 1</td>
        <td>linha 2, célula 2</td>
        </tr>
      </table>
   <div id="DataText" style="width: 200px; font-size: 10px;"></div>
</body>
</html>

1 answer

0


I think you can use the $.get, if the file is on the server.
Case local you are referring to the computer of who is accessing the page you will have to use another method, in that reply shows how to load data from a file without uploading to the server.
[Note]: In response to FileReader this using readAsDataURL you would have to use readAsText.

Example $.get

$.get( "registro0.txt", funcaoCallback);

function funcaoCallback( conteudo ) {
     /// Callback, chamada após o $.get pegar o
     ///  conteudo do arquivo aqui dentro voce
     ///  pode tratar o conteudo do arquivo

     console.log(conteudo);

     /// colocando os dados no elemento de id='DataText'
     $('#DataText').text( conteudo );

}

In one comment you said you were making a mistake XML Parser:.... Voce can try to put as last parameter of the call 'text' being like this:

$.get( "registro0.txt", funcaoCallback, 'text');
  • yes, it is a local file on my computer, the function is working, I can display the log in the div, the problem is I need to manipulate the data before displaying them, like create a filter of what will be displayed, this page will be rotated locally.

  • Then after the $.get take the data it calls a function of yours, in that function you can treat the data. In the example my function is just calling the console.log

  • 1

    I was able to solve the issue of the error by adding the "Plain" reference to the header,, however I have not yet obtained the result in reading the file, now I am getting error that the $ was not declared.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.