Jump to Navigation

017 - Ajax text return sample code

JavaScript generate an instance of a XMLHTTPRequest object and it can make a server request for text.

Example:

http = new XMLHttpRequest();

function getServerText() {
var myurl = 'textserver.php';
http.open("GET", url, true);
http.onreadystatechange = useHttpResponse;
http.send(null);
}

function useHttpResponse() {
if (http.readyState == 4) {
if (http.state == 200) {
var mytext = http.responseText;
document.getElementById('myPageElement').innerHTML = mytext;
}
} else {
document.getElementById('myPageElement').innerHTML = "";
}
}

JavaScript:


Main menu 2

Story | by Dr. Radut