ajax基本案例
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <script type="text/javascript"> function $(id){ return document.getElementById(id); } var xmlhttp=""; function get_xmlhttp(){
xmlhttp=new XMLHttpRequest(); var url='/ajax/test.php'; var data='username='+$('name').value; xmlhttp.open('post',url,true);//打开请求 xmlhttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded;"); xmlhttp.onreadystatechange=callback;//指定回调函数 xmlhttp.send(data);//发送数据,若get填null;若post,则填实际数据 } function callback(){ if (xmlhttp.readyState==4 && xmlhttp.status==200){ var obj=xmlhttp.responseText; obj=eval("("+obj+")"); document.getElementById('msg').value=obj.res; } } </script> </head> <body> <div id="container"> <div id="body"> <form action="#" method="post"> username:<input type="text" id="name" onKeyUp="get_xmlhttp();"/> password:<input type="password" id="pwd"/> <input type="submit" value="确定" onClick="get_xmlhttp();"/> <input type="text" id="msg"/> </form> </div> </body> </html> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |