日期:2014-05-16  浏览次数:20633 次

ajax小结
最近写了几个小的ajax例子,拿出来分享,高手请掠过,代码如下
var xmlHttpRequest;
function verify(){
    var userName = document.getElementById("userName").value;

    if (window.XMLHttpRequest) {
        xmlHttpRequest = new XMLHttpRequest();
    } else {
        if (window.ActiveXObject) {
            xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
    }

    //注册回调函数
    xmlHttpRequest.onreadystatechange = callback;

    //设置连接信息,用GET方式提交
    //xmlHttpRequest.open("GET","AJAXServlet?name=" + userName,true);

    //发送数据
    //xmlHttpRequest.send(null);

    //设置连接信息用POST方式提交
    xmlHttpRequest.open("POST","AJAXXMLServlet",true);
    //设置http的请求头
    xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    //xmlHttpRequest.setRequestHeader("Content-Type","application/x-www-form-urlencoded");


    xmlHttpRequest.send("name=" + userName);

}

function callback(){
    //判断对象交互是否完成
    if(xmlHttpRequest.readyState == 4){
        //判断http交互是否完成
        if(xmlHttpRequest.status == 200){
            var obj = xmlHttpRequest.responseXML;
            if(obj){
                var messageNodes = obj.getElementsByTagName("message");
                if(obj.length > 0){
                    var message = messageNodes[0].firstChild.nodeValue;
                    document.getElementById("result").innerHTML = message;
                }
            }else {
                window.alert("数据格式不正常");
            }

        }else {
            window.alert("出错");
        }

    }
}


服务器端代码
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.io.PrintWriter;

public class AJAXXMLServlet extends HttpServlet {
    protected void doPost(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        doGet(httpServletRequest,httpServletResponse);
    }

    protected void doGet(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws ServletException, IOException {
        httpServletRequest.setCharacterEncoding("UTF-8");
        httpServletResponse.setContentType("text/xml;charset=UTF-8");
        String name = httpServletRequest.getParameter("name");
        PrintWriter out =  httpServletResponse.getWriter();

        StringBuilder sb = new StringBuilder();
        sb.append("<message>");
        if(null == name || "" == name){
            sb.append("用户名不能为空").append("</message>");
            out.println(sb.toString());
        }else if("xiaoming".equals(name)){
            sb.append("用户名[" + name + "]已经存在").append("</message>");
            out.println(sb.toString());
        }else {
            sb.append("用户名[" + name + "]可以使用").append("</message>");
            out.println(sb.toString());
        }
        out.close();
    }
}



上面处理的是服务器端返回的xml格式的数据,在Servlet中要记得设置
httpServletResponse.setContentType("text/xml;charset=UTF-8");
即返回的文本格式需为xml