码迷,mamicode.com
首页 > Web开发 > 详细

ajax 异步处理 Get请求

时间:2017-12-03 20:54:02      阅读:240      评论:0      收藏:0      [点我收藏+]

标签:parameter   rms   int   send   设置   null   mic   val   microsoft   

1、js的编写

                            var xmlHttpRequest = null;
                            //1、创建XMLHttpRequest对象
                            if (window.XMLHttpRequest) {//新版本返回为TRUE
                                xmlHttpRequest = new XMLHttpRequest();
                            } else {
                                xmlHttpRequest = new ActiveXObject(
                                        "Microsoft.XMLHTTF");
                            }
                            //2、设置回调函数
                            xmlHttpRequest.onreadystatechange = callBack;
                            var username = $("#username").val();
                            //3、初始化XMLHttpRequest组件
                            var url = "UserServlet?username=" + username;
                            xmlHttpRequest.open("GET", url, true);
                            //4、发送请求
                            xmlHttpRequest.send(null);
                            //回调函数callBack的编写
                            function callBack() {
                                if (xmlHttpRequest.readyState == 4
                                        && xmlHttpRequest.status == 200) {
                                    var data = xmlHttpRequest.responseText;
                                    if (data == "true") {
                                        $("#errMsg").html("用户名已被注册");
                                    } else {
                                        $("#errMsg").html("用户可以注册");
                                    }
                                }
                            }
                        });

 

2、servlet的编写

package com.niu.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class UserServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        String name = req.getParameter("username");
        boolean used = false;
        if ("ajax".equals(name)) {
            used = true;
        }
        resp.setContentType("text/html;charset=utf-8");
        PrintWriter out = resp.getWriter();
        out.print(used);
        out.flush();
        out.close();
    }

}

 

ajax 异步处理 Get请求

标签:parameter   rms   int   send   设置   null   mic   val   microsoft   

原文地址:http://www.cnblogs.com/binglong180/p/7967083.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!