ajax请求内部实现

来源:互联网 发布:linux 修改时区 cst 编辑:程序博客网 时间:2024/06/10 00:05

<input  onclick="sendAjax">



<script type="text/javascript">
alert(111);

function sendAjax(){
    var xmlHttp = new XMLHttpRequest();
    
    xmlHttp.open("get","<%=basePath %>/manage/test/ajax",true);//true表示异步,false表示同步
    xmlHttp.send();//GET          xmlHttp.send("str");//POST
 

  //callbackFunction
    xmlHttp.onreadystatechange = function (){
            var state = xmlHttp.readyState;//状态4种
                                               var status = xmlHttp.status;//状态码
                                               if(state == 4 && status == 200){
                                                  var data=xmlHttp.responseText;//    还有responseXML
                                                   document.getElementById("ID").value = data;                          
                                               }
    }
}
</script>


后台处理:

@RequestMapping("/ajax")
    public void testAjax(HttpServletRequest request, HttpServletResponse response){
        
        try {
            
            response.setCharacterEncoding("utf-8");//响应字符集的编码格式
            PrintWriter out=response.getWriter();
            out.print("111");//发送响应数据
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        
    }

原创粉丝点击