0

Here is my javascript function that reads from the file every second and outputs it:

var timer;
var url = "http://.../testdata.txt";
function ajaxcall() {
    var lines;
    var alltext;
    request = new XMLHttpRequest();
    request.open("GET", url, true);
    request.onreadystatechange = function() {
        if (request.readyState === 4) {  // document is ready to parse.
            if (request.status === 200) {  // file is found
                allText = request.responseText;
                lines = request.responseText.split("\n");
                document.getElementById("test").innerHTML = "";
                for (i in lines) {
                    document.getElementById("test").innerHTML += lines[i] + "<br>";
                }
            }
        }
    }
    request.send();
}
timer = setInterval(ajaxcall, 1000);

I haven't got the hang of AJAX yet so I tried to make a similar way to write into the file using what I read on the internet:

function chat() {
    request = new XMLHttpRequest();
    request.open("POST", url, true);
    request.send("\n" + document.getElementById("chatbox").value);
}

However that does absolutely nothing and I don't understand why. The element "chatbox" is input type textbox and chat() is called by input type submit.

1

1 Answer 1

1

You cannot write to a file using just a POST call. In fact, you cant write to a file using only JavaScript/AJAX. You will need a server-side script in for example PHP that will write to the file for you, and then you need to call this script using AJAX.

Sign up to request clarification or add additional context in comments.

4 Comments

How would I go about doing that with Python which is installed on said server?
Unfortunately, I cannot help you do this in Python. You should probably rephrase your question and tag it 'Python'.
Did you try very hard to find this information yourself, Praxbyr? stackoverflow.com/search?q=python+write+file+ajax docs.python.org/2/tutorial/…
I didn't try VERY HARD but I did try and that is how I ended up with the "POST" method up above, seeing as it is wrong I will have to change that now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.