ajax post 使用小例子

2011-05-17 23:28  2480人阅读  评论 (0)

用了三年jquery,XMLHttpRequest怎么用给忘了,刚才写了一个例子,写成博文,以后也方便看。

<?php
if(empty($_REQUEST['a'])){
?>
<script>
var xmlhttp = null;
try{xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");}catch e){}
try{xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");}catch(e){}
try{xmlhttp = new XMLHttpRequest();}catch(e){}
//var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function(){
    if(this.readyState == 4){
        alert(xmlhttp.responseText);
        //console.log(xmlhttp.responseText);
    }
};
xmlhttp.open("POST", "xmlhttp.php", true);
xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;");
xmlhttp.send('a=a&b=b&c=c');
</script>
<?php
}else{
    echo json_encode($_REQUEST);
}
?>
豫ICP备09035262号-1