读书人

Ajax初记

发布时间: 2012-12-23 11:28:15 作者: rapoo

Ajax小记
使用Ajax的简单函数

var xmlhttp;   function useAjax(url, func, data) {              if (window.XMLHttpRequest) {           xmlhttp = new XMLHttpRequest();       } else {           xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");       }        xmlhttp.onreadystatechange = func;    xmlhttp.open("POST", url, true);        //Send the proper header information along with the requestxmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");xmlhttp.setRequestHeader("Content-length", data.length);xmlhttp.setRequestHeader("Connection", "close");    xmlhttp.send(data);}     function asynVisit(url) {var data = "name=smith&sex=male";    useAjax(url, function() {           if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {               alert(xmlhttp.responseText);           }       }, data);   }

读书人网 >Ajax

热点推荐