ITPub博客

首页 > IT职业 > IT职场 > ajax 学习

ajax 学习

IT职场 作者:zl_king 时间:2021-09-09 10:03:36 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=10document.write(eval(x+17))</script>
200427

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:

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/1567/viewspace-2815705/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2019-04-15

  • 博文量
    102
  • 访问量
    725