0

I have an html file with some javascript (NO jquery) that retrieves data from a server and stores it in a variable called sData_From_Server, which is shown on screen.

That data is saved in a file called DATA_BASE.txt, and it's a simple list of words that the user can change and send back to be saved in the server.

This is the code that I use to get that data from the server:

var sData_From_Server, sData_Entered_By_User;

fRetrieve_Data("DATA_BASE.txt", fCallback);

function fRetrieve_Data_From_Server(sFile, fFunction){
    let httpRequest = new XMLHttpRequest;
    httpRequest.open("GET", sFile);
    httpRequest.onload = fFunction;
    httpRequest.send();
}

function fCallback(){
    sData_From_Server = this.response;
    console.log(sData_From_Server);
    sData_Entered_By_User = sData_From_Server;
}

sData_Entered_By_User initially assumes the value of the previous stored data, and the user can change it at will (actually, all he can do is change the order of those words in that list).

Once the user is done he sends his updated list back to the server, and this is the code I use:

function fSend_Data_To_Server() {
    let xhr = new XMLHttpRequest();
    xhr.open("GET", "SAVE_DATA.php?sData_To_Be_Saved_By_PHP_File="+sData_Entered_By_User);
    xhr.onreadystatechange = function () {
        if (xhr.readyState == XMLHttpRequest.DONE) {
            console.log("responseText: " + xhr.responseText);
        }
    };
    xhr.send();
}

Everytime I run fSend_Data_To_Server() the file DATA_BASE.txt is updated in the server.

Everytime. There's NO error with this part of the code.

However, if I reload my page (or even if I close the browser and open it again) the content of sData_From_Server is not being updated.

I think there's something going on about the browser's cache, but I don't undestand why it's happening. I think it should not be happening.

Let's say the content of DATA_BASE.txt is A,B,C,D,XXXXXXXXXX.

When I first load the page I got that content in the variable sData_From_Server and I put it on the screen. It works perfectly.

Let's call it LIST ONE.

If I change that list to XXXXXXXXXX,A,B,C,D and send it to the server I can see that the content of DATA_BASE.txt is changed in the server. I do this checking through the server file manager.

Let's call it LIST TWO.

This new list is in my sreen at this time, but if I reload the page and therefore running fRetrieve_Data("DATA_BASE.txt", fCallback); again, the variable sData_From_Server assumes the value of LIST ONE.

How come???

If a open a different browser I can retrieve the correct data from DATA_BASE.txt, and this second browser shows me LIST TWO, but if I change the list and send it to the server (let's call it LIST THREE) this second browser won't update to this third list.

That means I have the file "DATA_BASE.txt correectly updated with LIST THREE, but the first browser keeps showing LIST ONE and the second browser keeps showing LIST TWO.

How can it be???

Even if I edit the file DATA_BASE.txt directly in the server and reload my browsers none of them will show me the right content at this time.

If I rename DATA_BASE.txt (on the server) to whatever other name, and then change througout the code to this new file name than I finally get the last list sent on screen (LIST THREE).

How come fRetrieve_Data_From_Server can not read the file on the server and get its content properly everytime???

Rbon
  • 152
  • 8
  • I know the duplicate is based on `fetch()` (highly recommend you switch to that btw) but some of the answers are still applicable to XHR, particularly the [headers one](https://stackoverflow.com/a/29246795/283366) and the [query parameter one](https://stackoverflow.com/a/59493583/283366) – Phil Mar 17 '23 at 03:35
  • I would also recommend you use a POST request to update the data – Phil Mar 17 '23 at 03:36

0 Answers0