当前位置: 首页 > news >正文

AJAX学习笔记5同步与异步理解

AJAX学习笔记4解决乱码问题_biubiubiu0706的博客-CSDN博客

示例

前端代码

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>演示AJAX同步和异步</title>
</head>
<body>
<script type="text/javascript">window.onload=function (){document.getElementById("btn1").onclick=function(){var xhr=new XMLHttpRequest();xhr.onreadystatechange=function(){if(this.readyState==4){if(this.status==200){document.getElementById("div1").innerHTML=this.responseText}else{alert("请求失败")}}}xhr.open("get","/ajax/test1",true)xhr.send()}document.getElementById("btn2").onclick=function(){var xhr=new XMLHttpRequest();xhr.onreadystatechange=function(){if(this.readyState==4){if(this.status==200){document.getElementById("div2").innerHTML=this.responseText}else{alert("请求失败")}}}xhr.open("get","/ajax/test2",false)xhr.send()}}
</script><button id="btn1">AJAX请求1</button>
<div id="div1"></div>
<button id="btn2">AJAX请求2</button>
<div id="div2"></div>
</body>
</html>

后端两个

test1

package com.web;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;/*** @author hrui* @date 2023/9/4 10:34*/
@WebServlet("/test1")
public class AjaxRequestTest1 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {try {Thread.sleep(10000);} catch (InterruptedException e) {e.printStackTrace();}resp.setContentType("text/html;charset=utf-8");resp.getWriter().print("AJAX请求1");}
}

test2

package com.web;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;/*** @author hrui* @date 2023/9/4 10:34*/
@WebServlet("/test2")
public class AjaxRequestTest2 extends HttpServlet {@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        try {
//            Thread.sleep(10000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }resp.setContentType("text/html;charset=utf-8");resp.getWriter().print("AJAX请求2");}
}

同步与异步的使用

当第一个按钮改成同步后    test1里面睡10秒

http://www.lryc.cn/news/163534.html

相关文章:

  • 911面试
  • 【Java基础篇 | 面向对象】—— 继承
  • DELL precision上安装nvidia A4000驱动 cuda cudnn
  • 数据结构算法刷题(29)动态规划
  • W11下CMake MinGW配置OpenCV和Qt
  • 反转字符串 反转字符串 || 反转字符串 |||
  • XML解析 不允许有匹配 _[xX][mM][lL]_ 的处理指令目标
  • 【C++进阶(五)】STL大法--list模拟实现以及list和vector的对比
  • Docker安装RabbitMQ集群_亲测成功
  • 50道基础数据结构面试题
  • 【Linux基础】权限管理
  • C++初阶--类和对象(中)
  • 【MySQL系列】视图特性
  • 管理类联考——数学——汇总篇——知识点突破——应用题——最值问题
  • 学习SpringMvc第二战之【SpringMVC之综合案例】
  • 【算法日志】单调栈: 单调栈简介及其应用
  • VSCode自动分析代码的插件
  • 设计模式之外观模式
  • Web端测试和 App端测试有何不同?
  • 12.(Python数模)(相关性分析一)相关系数矩阵
  • 系统架构设计师(第二版)学习笔记----嵌入式系统及软件
  • Python列表操作指南:索引、切片、遍历与综合应用
  • 第15章_锁: MySQL并发访问相同记录以及从数据操作的类型划分锁(读锁、写锁)
  • PHP 排序函数使用方法,按照字母排序等操作
  • windows本地验证码识别工具
  • 修改图片尺寸的几个简单方法
  • 三、GoLang字符串的基本操作
  • 基于vue-cli创建后台管理系统前端页面——element-ui,axios,跨域配置,布局初步,导航栏
  • 在 ubuntu20.04 上安装 Pytorch
  • 远程恋爱网站部署秘籍——群晖虚拟机助ni秀恩爱