不需要jQuery。之前写过一个,很简略,今天详细记录下:

<script type="text/javascript">
function getXmlDoc() {
var xmlhttp;
if (window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return xmlhttp;
}
function Get(url, callback) {
var xmlDoc = getXmlDoc();
xmlDoc.open('GET', url, true);
xmlDoc.onreadystatechange = function() {
if (xmlDoc.readyState === 4 && xmlDoc.status === 200) {
callback(xmlDoc);
}
}
xmlDoc.send();
}
function Post(url, data, callback) {
var xmlDoc = getXmlDoc();
xmlDoc.open('POST', url, true);
xmlDoc.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlDoc.onreadystatechange = function() {
if (xmlDoc.readyState === 4 && xmlDoc.status === 200) {
callback(xmlDoc);
}
}
xmlDoc.send(data);
}
function change(id) {
Get("http://test.com/log.php?id="+id,function(e){alert(e.responseText)});
Post("http://test.com/post.php","id="+id,function(e){alert(e.responseText)});
}
</script>

Related Posts: JavaScript-AJAX-GET-POST方式补记 :