jquery与ajax

简化GET请求与jQuery Ajax事件
本文介绍了一种简化GET请求的方法及jQuery中Ajax事件的使用。通过示例代码展示了如何进行GET请求,并介绍了jQuery提供的各种Ajax事件及其用途。

这是一个简单的 GET 请求功能以取代复杂 $.ajax 。请求成功时可调用回调函数。如果需要在出错时执行函数,请使用 $.ajax。示例代码:

$.ajax({

               type : "GET",

               contentType: "application/x-www-form-urlencoded; charset=utf-8",

               url : "ServiceServlet",

               data : "username="+username+"&password="+password,

               dataType : "xml",

               success : callback,

               error:callbackErr

           });

       下面是jQuery官方给出的完整的Ajax事件列表:

·  ajaxStart (Global Event)
This event is broadcast if an Ajax request is started and no other Ajax requests are currently running.

·         beforeSend (Local Event)
This event, which is triggered before an Ajax request is started, allows you to modify the XMLHttpRequest object (setting additional headers, if need be.)

·         ajaxSend (Global Event)
This global event is also triggered before the request is run.

·         success (Local Event)
This event is only called if the request was successful (no errors from the server, no errors with the data).

·         ajaxSuccess (Global Event)
This event is also only called if the request was successful.

·         error (Local Event)
This event is only called if an error occurred with the request (you can never have both an error and a success callback with a request).

·         ajaxError (Global Event)
This global event behaves the same as the local error event.

·         complete (Local Event)
This event is called regardless of if the request was successful, or not. You will always receive a complete callback, even for synchronous requests.

·         ajaxComplete (Global Event)
This event behaves the same as the complete event and will be triggered every time an Ajax request finishes.

·  ajaxStop (Global Event)
This global event is triggered if there are no more Ajax requests being processed.

具体的全局事件请参考API文档。

完整代码示例:前段

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript"  src="jquery-1.8.3.js"></script>
	<script>
	
    function doConfig(){
	   
	 //  var username = document.getElementById("username").value;
	 //  var password = document.getElementById("password").value;
	   var username = $("#username").val();
	   var password = $("#password").val();
	   $.ajax({
		        type : "GET",
		        contentType: "application/x-www-form-urlencoded; charset=utf-8",
		        url : "ServiceServlet",
		        data : "username="+username+"&password="+password,
		        dataType : "xml",
		        success : callback,
		        error:callbackErr
		    }); 
	   
	   }
	   function callback(data){
	  
	    var  name ="";
	     $(data).find('service').each(function(i){
	        name=$(this).find('info').text();
	      });
 		  alert(name);
	   }
	  function callbackErr(data){
	   
	   alert("error");
	   }
	</script>
	
	
  </head>
  
  <body>
    <table>
         <tr>
            <td>用户名:</td>
            <td><input id="username" /></td>
         </tr>
         <tr>
            <td>密码:</td>
            <td><input id="password" /></td>
         </tr>
         <tr>
            <td></td>
            <td><input type="submit" onclick="doConfig()" /></td>
         </tr>
    </table>
  </body>
</html>


后台代码:

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 ServiceServlet extends HttpServlet {

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

		   String username = request.getParameter("username");
		   String password = request.getParameter("password");
		   
		   System.out.println("用户名:"+username+"密码:"+password);
		   response.setContentType("text/xml");
		   response.setCharacterEncoding("utf-8");
		   response.getWriter().write("<?xml version=\"1.0\" encoding=\"utf-8\"?><service><info>success</info></service>");
		   
	}

	 
	public void init() throws ServletException {
	}

}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值