Index . 블로그창고 . Any comments?

\cat programming

AJAX:Getting Started

AJAX:Getting Started

<script type="text/javascript">
/* http://developer.mozilla.org/en/docs/AJAX:Getting_Started */
function makeRequest(url, data, func){
	var http_request = false;

	/* Create an XMLHTTP instance */
	if(window.XMLHttpRequest){ /* Mozilla, Safari, ... */
		http_request = new XMLHttpRequest();
		if(http_request.overrideMimeType){
			/* Some web servers return a non-standard mime type. */
			http_request.overrideMimeType('text/xml');
		}
	}else
	if(window.ActiveXObject){ /* IE */
		try{
			http_request = new ActiveXObject('Msxml2.XMLHTTP');
		}catch(e){
		try{
			http_request = new ActiveXObject('Microsoft.XMLHTTP');
		}catch(e){}
		}
	}
	if(!http_request){
		alert('Cannot create an XMLHTTP instance!');
		return false;
	}

	/* This function has no arguments. */
	http_request.onreadystatechange = function(){func(http_request);};

	if(data == null)
		var method = 'GET';
	else{
		var method = 'POST';
		http_request.setRequestHeader('Content-Type',
			'application/x-www-form-urlencoded');
	}

	/* http_request.open(method, url, asynchronous) */
	http_request.open(method, url, true);

	/* http_request.send(POST data) */
	/* required even if the method is not POST. */
	http_request.send(data);
}

function alertContents(http_request){
	/* http_request.readyState
	0:	uninitialized
	1:	loading
	2:	loaded
	3:	interactive
	4:	complete
	 */
	if(http_request.readyState != 4)
		return;
	if(http_request.status != 200){
		alert('There was a problem with the request.');
		return;
	}

	/* http_request.responseText: response as a string of text
	   http_request.responseXML:  response as an XMLDocument object
	<?xml version="1.0" ?>
	<root>
		I'm a test.
	</root>

	var xmldoc = http_request.responseXML;
	var root_node = xmldoc.getElementsByTagName('root').item(0);
	alert(root_node.firstChild.data);
	 */
	alert(http_request.responseText);
}
</script>
<span style="cursor:pointer; text-decoration:underline;"
	onclick="makeRequest('ajax_test.html', null, alertContents)">Make a request</span>

All the works in this site except software and copyrighted materials by others (e.g., 문학) are licensed under a Creative Commons License. 소프트웨어 및 문학과 같이 다른 이에게 저작권이 있는 작품을 제외하고 모두 크리에이티브 커먼즈 라이센스를 따른다.
Mon Mar 25 01:34:33 2024 . XHTML . CSS (lightbox.css is not part of Uniqki. ;-) . Powered by Uniqki!
refresh . edit . loginout . index