【入门笔记】一小时学完AJAX(仅供本人笔记,如学习,请移步专门的教程)

【完整例子】(来自网络)

function loadXMLDoc()
{
  var xmlhttp;
  if (window.XMLHttpRequest)
  {
    // IE7+, Firefox, Chrome, Opera, Safari 浏览器执行代码
    xmlhttp=new XMLHttpRequest();
  }
  else
  {
    // IE6, IE5 浏览器执行代码
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function()
  {
    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
      document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open("POST","/try/ajax/demo_post2.php",true);
  xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  xmlhttp.send("fname=Henry&lname=Ford");
}


【1.避免缓存】xmlhttp.open("GET","/try/ajax/demo_get.php?t=" + Math.random(),true);

【2.核心语句】

get方法核心语句:

xmlhttp.open("GET","/try/ajax/demo_get2.php?fname=Henry&lname=Ford",true);

xmlhttp.send();

post核心:

  xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
  xmlhttp.send("fname=Henry&lname=Ford");请求被发送到服务器

Content-type是网页头的一部分。浏览器把form数据封装到http body中,然后发送到server。

【3.】

async [ə'zɪŋk]
T/F? false等待执行脚本,true则不等待服务端返回,继续执行后面。
参考:http://blog.csdn.net/yanfangphp/article/details/8261333

【4.responseText接受后台值

【5.】onreadystatechange 事件(请求被发送到服务器

【6.回调函数】用于多个AJAX检测


【感想】还是比较简单的。一会就看完了。虽说没有jsp实例比较烦(百度下应该有)。

语法简单。封装的不错。有空复习、深入下。


x-www-form-urlencoded

猜你喜欢

转载自blog.csdn.net/chanlingmai5374/article/details/78648076