jquery封裝的ajax的請求

來源:互聯網
上載者:User

//頁面的代碼

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<script type="text/javascript" src="js/jquery-1.2.6.js"></script>
<%
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">
 
  onload = function()
  {
   //1、這是用jquery的ajax的post請求
   //第一個參數是請求的路徑,第二個參數是請求帶有的參數,第三個是一個回呼函數,回呼函數的第一個參數是返回的資料,第二個參數是是否
   //請求成功並且返回了資料,這裡的get請求與post請求的方式一樣,只不過將get改成post即可,但是這裡一定要注意,juqery的ajax的get、
   //post請求都會產生緩衝,所以在後台一定要處理好緩衝的問題,這種方式的請求的底層都是基於$.ajax
   $.post("test?p=test",{"name":"caohuan","age":12},function(data, status){
    //這是判斷請求是否成功並且返回了資料
    if(status == 'success')
    {
     alert(data);
    }else
    {
     alert("ajax失敗");
    }
  
   });
   //這種方式的請求的get和post的請求的方式差不多,只不過將post改成get就行了,還有一點值得注意,這種方式請求,我們不需要擔心緩衝的問題
   $.ajax({
    type:'post',//請求的方式
    url:'test?p=test',//請求的路徑
    dateType:'html',//這句話必須要寫上,因為對於Firefox而言,必須要加上這句代碼才能正常運行
    data:{'name':'caohuan','age':21},//請求帶上的參數
    success:function(data){//請求成功返回的資料
     alert(data);
    }
   });
  }
 </script>
 
  </head>
 
  <body>
    This is my JSP page. <br>
  </body>
</html>
 

//背景代碼

package cn.itcast.servlets;


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 AjaxTestServlet extends HttpServlet {
 
 @Override
 protected void service(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
   request.setCharacterEncoding("UTF-8");
   response.setContentType("text/html;charset=utf-8");
   //下面三句代碼是處理緩衝的問題
   response.setHeader("pragma", "no-cache");
   response.setHeader("cache-control", "no-cache");
   response.setHeader("expires", "0");
   String p = request.getParameter("p");
   if("test".equals(p))
   {
    doTest(request, response);
   }
 }
 
 protected void doTest(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
  String name = request.getParameter("name");
  String age = request.getParameter("age");
  PrintWriter out = response.getWriter();
  out.print(name+"-->" +age);
  out.flush();
 www.2cto.com
 
 }
 

}
作者:ch469299503
 


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.