ajax_02-结合jQuery简单的get与post请求
Posted ys15
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ajax_02-结合jQuery简单的get与post请求相关的知识,希望对你有一定的参考价值。
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请求的主要内容,如果未能解决你的问题,请参考以下文章
jquery中ajax方法load get post与脚本文件如php脚本连接时,脚本怎样接受数据?