//ajaxtest.js
function getHTTPObject() {
var xhr = false;
if (window.XMLHttpRequest)
{
xhr = new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
try {
xhr = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e) {
try {
xhr = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e) {
xhr = false;
}
}
}
return xhr;
}
function getFile(file) {
var request = getHTTPObject();
if (request) {
request.onreadystatechange = function() {
displayResponse(request);
};
request.open("GET", file, true);
request.send(null);
}
}
function displayResponse(request) {
if (request.readyState == 4) {
if (request.status == 200 || request.status == 304) {
alert(request.responseText);
}
}
}
<html>
<head>
<title>Using XMLHttpRequest<⁄title>
<script type=""text/javascript" src="ajaxtext.js"><⁄script>
<⁄head>
<body>
<p>
<a href=""message.txt" onclick="getFile(this.href);
return false;">
Click here to see the contents of a text file
<⁄a>
<⁄p>
<⁄body>
<⁄html>
2 comments:
Should I use an HTTP GET or POST for my AJAX calls?
AJAX requests should use an HTTP GET request when retrieving data where the data will not change for a given request URL. An HTTP POST should be used when state is updated on the server. This is in line with HTTP idempotency recommendations and is highly recommended for a consistent web application architecture
Post a Comment