码迷,mamicode.com
首页 > Web开发 > 详细

ajax_02-结合jQuery简单的get与post请求

时间:2019-07-07 17:24:49      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:callback   url   style   jsp页面   col   put   throw   cti   doget   

1、jsp页面

 1 <%@ page language="java" contentType="text/html; charset=UTF-8"
 2     pageEncoding="UTF-8"%>
 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4 <html>
 5 <head>
 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 7 <%
 8 pageContext.setAttribute("APP_PATH", request.getContextPath());
 9 %>
10 <script type="text/javascript" src="js/jquery-1.11.3.min.js"></script>
11 <title>Insert title here</title>
12 </head>
13 <body>
14 
15     <input type="button" onclick="get()" value="使用JQuery演示 get方法"/>
16     <input type="button" onclick="post()" value="使用JQuery演示 post方法"/>
17      <div id="div01"  style="width: 100px ; height: 100px ; border: 1px solid blue; ">
18      </div>
19       <div id="div02"  style="width: 100px ; height: 100px ; border: 1px solid blue; ">
20      </div>
21 </body>
22 <script type="text/javascript">
23 //$.get(URL,callback);
24     function get() {
25         $.get("/day16/DemoServlet02?name=aa&age=19"  , function(data ,status) {
26             alert("结果是:"+data);
27             $("#div01").text("aaa="+data);
28         });
29     }
30     function post(){
31         $.post("${APP_PATH}/DemoServlet02?name=aa&age=19",function(data,status){
32             $("#div02").text("post="+data);
33         });
34     }
35 </script>
36 </html>

关键点:$.get("url",function(data,status));其中data就是后台传过来的数据 将get换为post即为post请求

2、后台java代码

 1 package com.itheima.servlet;
 2 
 3 import java.io.IOException;
 4 import javax.servlet.ServletException;
 5 import javax.servlet.http.HttpServlet;
 6 import javax.servlet.http.HttpServletRequest;
 7 import javax.servlet.http.HttpServletResponse;
 8 
 9 /**
10  * Servlet implementation class DemoServlet02
11  */
12 public class DemoServlet02 extends HttpServlet {
13     protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
14 
15         String name = request.getParameter("name");
16         int age = Integer.parseInt(request.getParameter("age"));
17         System.out.println("收到了请求..."+name+"="+age);
18         
19         response.setContentType("text/html;charset=utf-8");
20         response.getWriter().write("给你一份数据");
21     }
22 
23     /**
24      * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
25      */
26     protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
27         // TODO Auto-generated method stub
28         System.out.println("将请求转给get处理");
29         doGet(request, response);
30     }
31 
32 }

 

ajax_02-结合jQuery简单的get与post请求

标签:callback   url   style   jsp页面   col   put   throw   cti   doget   

原文地址:https://www.cnblogs.com/ys15/p/11146772.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!