74.Ajax請求(js版本)

1.爲何要使用Ajaxjavascript

  優勢: 局部刷新    提升用戶體驗html

2.Ajax發出請求的demo前端

這裏使用servlet 到後臺java

2.1使用到的技術jquery

(1)jqueryajax

(2)Ajax服務器

(3)servletjsp

(4) 輸出流 PrintWriter out = response.getWriter()ui

2.2 實現過程spa

2.2.1 前端

新建jsp頁面

編寫一個頁面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript" src="./js/jquery-1.8.3.js"></script>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>ajax入門案例</title>
<script>
            $(function(){
                //光標離開事件
                $("#userName").blur(function(){
                        var userName =    $("#userName").val();
                        //建立XmlHttpRequest對象
                        var xmlhttp;
                        if (window.XMLHttpRequest)
                          {// code for IE7+, Firefox, Chrome, Opera, Safari
                          xmlhttp=new XMLHttpRequest();
                          }
                        else
                          {// code for IE6, IE5
                          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
                          }
                        //建立鏈接
                        xmlhttp.open("GET","${pageContext.request.contextPath }/ajaxTest?userName="+userName);
                        //發送請求
                        xmlhttp.send();
                        //使用事件獲得響應數據,並處理結果
                        xmlhttp.onreadystatechange=function(){
                          //xmlhttp.readyState==4 表示客戶端請求一切正常
                          //xmlhttp.status==200 表示服務器端響應一切正常
                          //alert(xmlhttp.readyState);
                          //alert(xmlhttp.status);
                          if (xmlhttp.readyState==4 && xmlhttp.status==200){
                            //document.getElementById("Prompt").innerHTML=xmlhttp.responseText;//得到服務器響應正文
                            $("#Prompt").html(xmlhttp.responseText);//得到服務器響應正文
                          }
                        }
                })
                
            });
        </script>
</head>
<body>
    <!--http://localhost:8080/day22_ajax_01/regist.jsp -->
    用戶名:<input type="text" name="userName"  id="userName"><span id="Prompt"></span><br />
    用戶名2:<input type="text" name="password"  id="password" placeholder="比較框"><br />
</body>
</html>

2.2.2  後臺

package cn.ma.ajax;

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;

/**
 * 光標離開 發出Ajax請求
 * http://localhost:8080/day22_ajax_01/regist.jsp
 */
public class AjaxTest extends HttpServlet {
    private static final long serialVersionUID = 1L;
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            
        response.setCharacterEncoding("UTF-8");
        String userName = request.getParameter("userName");
            PrintWriter out = response.getWriter();
            if("張三".equals(userName)){
                
                out.write("該帳號已被註冊");
            }else{
                out.write("該帳號可用");
            }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
3. 效果

相關文章
相關標籤/搜索