当前位置: 代码迷 >> Ajax >> JQuery-$.ajax(get)步骤
  详细解决方案

JQuery-$.ajax(get)步骤

热度:612   发布时间:2012-10-12 10:17:04.0
JQuery--$.ajax(get)方法
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <head>
        <title>ajax-get().html</title>
        <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
        <meta http-equiv="description" content="this is my page">
        <meta http-equiv="content-type" content="text/html; charset=UTF-8">
        <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
    
	<script language="JavaScript" src="jquery-1.6.1.js"></script>
	<script language="JavaScript">
		$(function(){
			$("#bt1").click(function(){
				var name =$("#tx1").val();
				var URL = "../validate?name="+name+"&date="+new Date().getTime();
				$.ajax({
					type:"GET",
					url:URL,
					dataType:"xml",
					success:function(msg){
						alert(msg);
						var jqueryObj =$(msg) ;//把后台传来的对象转为JQuery对象
						alert(jqueryObj);
						var content = jqueryObj.text();
						$("#sp1").text(content);
						
					}
				});
			})
		})
	</script>
	</head>
	<body>
    <p align="center">
      6-2 用户注册
    </p>
    <table align="center">
        <tr>
            <td>
                用户名
            </td>
            <td>
                <input type="text" id="tx1">
				<input type="button"  id="bt1" value="检测">
            </td>
            <td>
                <span id="sp1"></span>
            </td>
        </tr>
    </table>
    </body>
</html>

?后台服务器代码:

package com.wepull.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;

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

public class ValidateServlet extends HttpServlet {

	
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doProcess(request,  response);
	}




	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doProcess(request,  response);
	}
	private void doProcess(HttpServletRequest request,
			HttpServletResponse response) {
	try {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String name = request.getParameter("name");
		System.out.println("ajax传来的name:"+name);
	    name = URLDecoder.decode(name,"utf-8");

		PrintWriter  pw =	response.getWriter();
		if("w".equals(name)){
			pw.println("<root>"+name+":已经被占用"+"</root>");
			
		}else{
			pw.println("<root>"+name+":可以注册"+"</root>");
			
		}
		
	} catch (IOException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	}

}



?

  相关解决方案