JavaScript DHTML/Ajax Layer/XMLHttpRequest

Материал из Web эксперт
Перейти к: навигация, поиск

Ajax and php

 
<html>
<head>
<title></title>
</head>
<body>
<div id="xmldata"></div>
<script type="text/javascript">
function readyAJAX() {
try {
    return new XMLHttpRequest();
} catch(e) {
    try {
        return new ActiveXObject("Msxml2.XMLHTTP");
    } catch(e) {
        try {
            return new ActiveXObject("Microsoft.XMLHTTP");
        } catch(e) {
            return "A newer browser is needed.";
        }
    }
}
}
var requestObj = readyAJAX();
var url ="http://127.0.0.1/sum.php?num1=2&num2=2";
requestObj.open("GET",url,true);
requestObj.send();
requestObj.onreadystatechange = function() {
    if (requestObj.readyState == 4) {
        if (requestObj.status == 200) {
            document.write(requestObj.responseText);
        } else {
            document.write(requestObj.statusText);
        }
    }
}
</script>
</body>
</html>

//sum.php
<?php
print $_GET["num1"] + $_GET["num2"];
?>



Is your Browser Ajax ready

 
<html>
<head>
<title>ISBN</title>
</head>
<body>
<div id="data"></div>
<script type="text/javascript">
function readyAJAX() {
    try {
        return new XMLHttpRequest();
    } catch(e) {
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                return new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                return "A newer browser is needed.";
            }
        }
    }
}
var requestObj = readyAJAX();
var url = "http://www.yourserver.org/isbn.php?isbn=11234567890";
    requestObj.open("GET",url,true);
    requestObj.send();
    requestObj.onreadystatechange = function() {
    if (requestObj.readyState == 4) {
        if (requestObj.status == 200) {
            document.write(requestObj.responseText);
        } else {
            document.write(requestObj.statusText);
        }
    }
}
</script>
</body>
</html>



Use XMLHttpRequest to send and read data

 
<html>
<head>
<title>ISBN</title>
</head>
<body>
<div id="data"></div>
<script type="text/javascript">
function readyAJAX() {
    try {
        return new XMLHttpRequest();
    } catch(e) {
        try {
            return new ActiveXObject("Msxml2.XMLHTTP");
        } catch(e) {
            try {
                return new ActiveXObject("Microsoft.XMLHTTP");
            } catch(e) {
                return "A newer browser is needed.";
            }
        }
    }
}
var requestObj = readyAJAX();
var url = "http://www.yourserver.org/isbn.php?isbn=11234567890";
requestObj.open("GET",url,true);
requestObj.send();
requestObj.onreadystatechange = function() {
    if (requestObj.readyState == 4) {
        if (requestObj.status == 200) {
            document.write(requestObj.responseText);
        } else {
            document.write(requestObj.statusText);
        }
    }
}
</script>
</body>
</html>