jquery对ajax进行了封装,非常方便。
自己用$.get()方法写了个小demo,包括客户端和服务端。
客户端:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="../js/jquery-3.1.0.min.js"></script> </head> <body> <div id="div"> <div > <span class="f1">电影</span> <span class="f2"><input id="btn" type="button" value="加载" ></span> </div> <ul></ul> </div> <script type="text/javascript"> $(function(){ $("#btn").bind("click",function(){ var $this= $(this); $.get("../jsp/test4.jsp",function(data){ $this.attr("disabled","true"); $("ul").append("<li>"+ data+ "</li>"); }); }); }); </script> </body> </html>
服务器端(jsp):
<%@page import="org.json.simple.JSONObject"%> <%@ page language="java" contentType="text/html; charset=utf-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% String msg= "你好吗?ajax"; response.getWriter().print(msg); %> </body> </html>
时间: 2024-11-05 16:12:30