首页 > Web开发 > 详细

ajax 学习

时间:2017-07-29 17:12:23      阅读:194      评论:0      收藏:0      [点我收藏+]

js eval函数

eval() 函数可计算某个字符串,并执行其中的的 JavaScript 代码。

<script type="text/javascript">

eval("x=10;y=20;document.write(x*y)")

document.write(eval("2+2"))

var x=10
document.write(eval(x+17))

</script>
200
4
27

ajax 利用XMLHttpRequest对象,进行异步或者同步请求。

  function createXMLHtppRequest()
  {
    if(window.ActiveXOject)
    {
     xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    else if(window.XMLHttpRequest)
    {
      xmlhttp = new XMLHttpRequest();
    }
  }

方法获得一个XMLHttpRequest对象。

xmlhttp.open("GET","test1.txt",true);
xmlhttp.send();

向服务器发送请求。

xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","test1.txt",true);
xmlhttp.send();

当使用 async=true 时,请规定在响应处于 onreadystatechange 事件中的就绪状态时执行的函数:


xmlhttp.open("GET","test1.txt",false);
xmlhttp.send();
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;

不推荐使用false:

ajax 学习

原文:http://10750710.blog.51cto.com/10740710/1951966

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!