本文共 1765 字,大约阅读时间需要 5 分钟。
原生的JS实现方式
package com.xww.web.servlet;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;@WebServlet("/ajaxServlet")public class AjaxServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //1.获取请求参数 String username = request.getParameter("username"); //处理业务逻辑。耗时 try { Thread.sleep(5000); } catch (InterruptedException e) { e.printStackTrace(); } //2.打印username System.out.println(username); //3.响应 response.getWriter().write("hello : " + username); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); }}
$.ajax()
$.ajax({键值对});
ajax
$.get()
:发送get请求 $.get(url, [data], [callback], [type])
get
$.post()
:发送post请求 $.post(url, [data], [callback], [type])
post
转载地址:http://hugwz.baihongyu.com/