Ajax知识总结

Ajax的目的就是让页面“自动”的与后台实现交互。

一、全部的现代浏览器均支持XMLHttpRequest对象。建立XMLHttpRequest用
     var xmlRequest=new XMLHttpRequest();

二、服务器请求:
     GET请求:
     xmlRequest.open("GET","1.servlet",true);
     xmlRequest.send();
 
方法                                              描述
 
   
open(method,url,async)

规定请求的类型、URL 以及是否异步处理请求。javascript

  • method:请求的类型;GET 或 POST
  • url:文件在服务器上的位置
  • async:true(异步)或 false(同步)
send(string)

将请求发送到服务器。html

  • string:仅用于 POST 请求
    

POST请求:
     xmlRequest.open("post","1.servlet",true);
     xmlhttp.setRequestHeader("Content-type"."application/***")
     xmlhttp.send("hello");
 
方法 描述
setRequestHeader(header,value)

向请求添加 HTTP 头。java

  • header: 规定头的名称
 
XMLHttpRequest 对象若是要用于 AJAX 的话,其 open() 方法的 async 参数必须设置为 true:
当使用 async=true 时,请规定在响应处于 onreadystatechange 事件中的就绪状态时执行的函数:
例子:
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for 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("GET","/ajax/test1.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">经过 AJAX 改变内容</button>

</body>
</html>
 

三、服务器的响应:
属性                     描述
   
responseText 得到字符串形式的响应数据。
responseXML 得到 XML 形式的响应数据。
 
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
 

 
四、 XMLHttpRequest 对象的三个重要的属性:
属性 描述
onreadystatechange 存储函数(或函数名),每当 readyState 属性改变时,就会调用该函数。
readyState

存有 XMLHttpRequest 的状态。从 0 到 4 发生变化。ajax

  • 0: 请求未初始化
  • 1: 服务器链接已创建
  • 2: 请求已接收
  • 3: 请求处理中
  • 4: 请求已完成,且响应已就绪
status

200: "OK"浏览器

 
404: 未找到页面
 
<html>
<head>
<script type="text/javascript">
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for 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("GET","/ajax/test1.txt",true);
xmlhttp.send();
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="loadXMLDoc()">经过 AJAX 改变内容</button>

</body>
</html>
 

或者,多个myFunction()时应该这样作
<html>
<head>
<script type="text/javascript">
var xmlhttp;
function loadXMLDoc(url,cfunc)
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=cfunc;
xmlhttp.open("GET",url,true);
xmlhttp.send();
}
function myFunction()
{
loadXMLDoc("/ajax/test1.txt",function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
    }
  });
}
</script>
</head>
<body>

<div id="myDiv"><h2>Let AJAX change this text</h2></div>
<button type="button" onclick="myFunction()">经过 AJAX 改变内容</button>

</body>
</html>
 

jQuery中的Ajax:
从上面能够看出,Ajax只须要了解浏览器与服务器的GET、POST请求,接收服务器的返回消息responseText和responseXML,以及在接收到消息后须要作什么事情readystateonchange既能够了。这些在加jQuery中也有很好的支持。
这是jQuery的语法,只须要这么一句话就完成了上面的请求,接收消息以及接收消息后作什么事情。
$( selector).load( URL, data, callback);

必需的 URL 参数规定您但愿加载的 URL。服务器

可选的 data 参数规定与请求一同发送的查询字符串键/值对集合。app

可选的  callback 参数是 load() 方法完成后所执行的函数名称。
例子:

这是示例文件("demo_test.txt")的内容:异步

<h2>jQuery and AJAX is FUN!!!</h2>
<p id="p1">This is some text in a paragraph.</p>
$("#div1").load("demo_test.txt");//执行后会将demo_test.txt的内容写到class为div1的div中
 
也能够把 jQuery 选择器添加到 URL 参数。
下面的例子把 "demo_test.txt" 文件中 id="p1" 的元素的内容,加载到指定的 <div> 元素中:
$("#div1").load("demo_test.txt #p1");

可选的 callback 参数规定当 load() 方法完成后所要容许的回调函数。回调函数能够设置不一样的参数:async

  • responseTxt - 包含调用成功时的结果内容
  • statusTXT - 包含调用的状态
  • xhr - 包含 XMLHttpRequest 对象
下面的例子会在 load() 方法完成后显示一个提示框。若是 load() 方法已成功,则显示“外部内容加载成功!”,而若是失败,则显示错误消息:
$("button").click(function(){
  $("#div1").load("demo_test.txt",function(responseTxt,statusTxt,xhr){
    if(statusTxt=="success")
      alert("外部内容加载成功!");
    if(statusTxt=="error")
      alert("Error: "+xhr.status+": "+xhr.statusText);
  });
});
但有时候咱们也须要向Ajax中同样对服务器作GET或者是POST请求,此时就需用jQuery的下面方法了。
$.get( URL, callback);
必需的  URL 参数规定您但愿请求的 URL。
可选的  callback 参数是请求成功后所执行的函数名。第一个回调参数是服务器返回的内容,第二个回调参数是请求的状态码。
$.post( URL, data, callback);

必需的 URL 参数规定您但愿请求的 URL。函数

可选的 data 参数规定连同请求发送的数据。

可选的  callback 参数是请求成功后所执行的函数名。第一个回调参数是服务器返回的内容,第二个回调参数是请求的状态码。
相关文章
相关标签/搜索