Ajax

  • Ajax

ajax核心:XMLHttpRequest对象无需刷新页面即可从服务器取得数据。

  1. 创建XMLHttpRequest对象。
    var xml=new XMLHttpRequest();
  2. XMLHttpRequest的用法
    xml.open("get","example.php",false);

    首先调用open()方法,接受的三个参数值:要发送的请求类型(get,post等),请求的URL,是否异步发送请求的布尔值。此时,会启动针对example.php的get请求。

  3. send() 接受一个参数:作为请求主体发送的数据。null为不需要请求主体发送数据。在收到响应后,响应数据会自动填充xml对象的属性:responseText,responseXML,status(http响应状态码),statusText(http状态说明).
     1 <script type="text/javascript">
     2     var xml=new XMLHttpRequest();
     3     xml.open("get","1.html",false);
     4     xml.send(null);
     5     if(xml.status>=20&&xml.status<300||xml.status==304){
     6         alert(xml.responseText);
     7     }else{
     8         alert(xml.status);
     9     }
    10 </script>
  4.  post 和get 请求

        get是最常见的请求类型,常用域向服务器查询某些信息。必要时将查询字符串参数追加到URL末尾,以便将信息发送给服务器。名-值之间用&分割。

        post请求:向服务器发送应该被保存的数据。把数据作为请求的主体提交。

原文地址:https://www.cnblogs.com/alaner/p/9526227.html