加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

第一个 Ajax 入门实例

发布时间:2020-12-16 00:18:22 所属栏目:百科 来源:网络整理
导读:http://blog.csdn.net/liruxing1715/article/details/7104404 html head titleAjax/title scriptlanguage= "javascript" var xmlHttp; //定义一个全局变量xmlHttp //创建XMLHttpRequest对象 function createXMLHttpRequest(){ if (window.ActiveXObject){ xm

http://blog.csdn.net/liruxing1715/article/details/7104404


  1. <html>
  2. <head>
  3. <title>Ajax</title>
  4. <scriptlanguage="javascript">
  5. varxmlHttp;//定义一个全局变量xmlHttp
  6. //创建XMLHttpRequest对象
  7. functioncreateXMLHttpRequest(){
  8. if(window.ActiveXObject){
  9. xmlHttp=newActiveXObject("Microsoft.XMLHTTP");//IE
  10. }elseif(window.XMLHttpRequest){
  11. newXMLHttpRequest();//XMLHttpRequest
  12. }
  13. }
  14. functionstartRequest(){
  15. createXMLHttpRequest();//创建XMLHttpRequest
  16. varurl='index.php?'+newDate().getTime();//异步传输到的页面index.php,后面加newDate().getTime(),是为了消除IE的缓存
  17. xmlHttp.open('GET',url,true);//open()方法发送一个异步请求
  18. //属性变化了就会触发onreadystatechange事件
  19. xmlHttp.onreadystatechange=function(){
  20. //如果请求的状态等于4(接受成功),并且服务器返回的HTTP响应值为200(请求成功)
  21. if(xmlHttp.readyState==4&&xmlHttp.status==200){
  22. //弹出服务器返回的文本
  23. alert('服务器返回:'+xmlHttp.responseText);
  24. xmlHttp.send(null);
  25. </script>
  26. </head>
  27. <body>
  28. <inputtype="button"value="异步通讯"onClick="startRequest();"/>
  29. </body>
  30. </html>

index.php

[php] view plain copy
    <?php
  1. header('Content-type:text/html;Charset=GB2312');
  2. echo'异步测试成功!';
  3. ?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读