ajax

来源:互联网 发布:java实现html转pdf 编辑:程序博客网 时间:2024/04/27 17:32

AJAX

var xmlhttp;

  1. 创建请求对象
    if(window.XMLHttpRequest){
    // code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }else{
    // code for IE6, IE5
    xmlhttp=new ActiveXObject(“Microsoft.XMLHTTP”);
    }

  2. 设置回调函数(监听)
    xmlhttp.onreadystatechange=函数名;

    xmlhttp.onreadystatechange=function(){
    函数体。。。
    }

  3. 初始化:
    xmlhttp.open(“GET”,”gethint.php?q=”+str,true); //异步以get方式发送到gethint.php

  4. 发送:
    xmlhttp.send();

其中:xmlhttp请求对象:
属性:
readyState //请求状态:0,1,2,3,4
*responseText//响应内容
responseXML //xml响应对象
*status //浏览器响应状态:200正常, 404 请求地址不存在 ,,
statusText //状态内容
*onreadystatechange //回调函数属性

方法:
abort() //取消当前响应,关闭连接并且结束任何未决的网络活动。
getAllResponseHeaders() //把 HTTP 响应头部作为未解析的字符串返回。
getResponseHeader() //返回指定的 HTTP 响应头部的值
*open() //初始化 HTTP 请求参数
*send() //发送 HTTP 请求,使用传递给 open() 方法的参数
*setRequestHeader() //向一个打开但未发送的请求设置或添加一个 HTTP 请求。

模拟POST提交代码:
xmlhttp.open(“POST”,”ajax_test.php”,true);
xmlhttp.setRequestHeader(“Content-type”,”application/x-www-form-urlencoded”);
xmlhttp.send(“fname=Bill&lname=Gates”);

0 0