自己封装Ajax类
来源:互联网 发布:开淘宝店怎么卖汉服 编辑:程序博客网 时间:2024/04/28 23:34
function GetAjax(callback){var xmlhttp = null;if(window.XMLHttpRequest){ xmlhttp=new XMLHttpRequest(); }else{ // code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } xmlhttp.onreadystatechange = function(){ //当请求状态为4时,响应状态为200时 callback(xmlhttp); } return xmlhttp;}
<html><script type="text/javascript" src="js/usefull.js"></script><script>function myclick(){var xmlhttp = GetAjax(function(obj){if(obj.readyState==4 && obj.status==200){var txt = obj.responseText;alert(txt);var json = eval("(" + txt +")");alert(json[0].name + json[1].name + json[2].name +json[3].name);}});xmlhttp.open("GET","hello.asp",true);xmlhttp.send();} </script><body><button type="button" onclick="myclick()">点击</button></body></html>