• Ajax&Axios 服务器渲染&异步的基本使用


    目录

    一、Ajax

    1.1 服务器端渲染

    1.2 Ajax渲染(局部更新)

    1.3 前后端分离

    1.4 同步与异步

    1.4.1 同步

    1.4.2 异步

     二、Axios

    2.1 Axios简介

    2.2 Axios基本用法

    2.2.1 在前端页面引入开发环境

    2.2.2 发送普通请求参数

    2.3 服务器端返回JSON数据

    2.3.1 前端代码

    2.3.2 后端代码


    一、Ajax

    1.1 服务器端渲染

    1.2 Ajax渲染(局部更新)

    1.3 前后端分离

            真正的前后端分离是前端项目和后端项目分服务器部署,在我们这里我们先理解为彻底舍弃服务器端渲染,数据全部通过Ajax方式以JSON格式来传递

    1.4 同步与异步

            Ajax本身就是Asynchronous JavaScript And XML的缩写,直译为:异步的JavaScript和XML。在实际应用中Ajax指的是:不刷新浏览器窗口,不做页面跳转,局部更新页面内容的技术。

    『同步』异步』是一对相对的概念,那么什么是同步,什么是异步呢?

    1.4.1 同步

    多个操作按顺序执行,前面的操作没有完成,后面的操作就必须等待。所以同步操作通常是串行的。

    1.4.2 异步

     多个操作相继开始并发执行,即使开始的先后顺序不同,但是由于它们各自是在自己独立的进程或线程中完成,所以互不干扰,谁也不用等

     二、Axios

    2.1 Axios简介

            使用原生的JavaScript程序执行Ajax极其繁琐,所以一定要使用框架来完成。而Axios就是目前最流行的前端Ajax框架。

    Axios官网:axios中文网|axios API 中文文档 | axios

    使用Axios和使用Vue一样,导入对应的*.js文件即可。官方提供的script标签引入方式为:

     我们可以把这个axios.min.js文件下载下来保存到本地来使用。

    2.2 Axios基本用法

    2.2.1 在前端页面引入开发环境

    1. <script type="text/javascript" src="/demo/static/axios.min.js">script>

    2.2.2 发送普通请求参数

    ① 前端代码

    HTML标签:

    1. <div id="app">
    2. <button @click="commonParam">普通请求参数button>
    3. div>

    Vue+axios代码:

    1. var vue = new Vue({
    2. "el":"#app",
    3. "data":{
    4. "message":""
    5. },
    6. "methods":{
    7. commonParam(){
    8. //使用axios发送异步请求
    9. axios({
    10. "method":"post",
    11. "url":"demo01",
    12. "params":{
    13. "userName":"tom",
    14. "userPwd":"123456"
    15. }
    16. }).then(response => {
    17. //then里面是处理请求成功的响应数据
    18. //response就是服务器端的响应数据,是json类型的
    19. //response里面的data就是响应体的数据
    20. this.message = response.data
    21. }).catch(error => {
    22. //error是请求失败的错误描述
    23. console.log(error)
    24. })
    25. }
    26. }
    27. })

    ② 后端代码

    1. public class ServletDemo01 extends HttpServlet {
    2. @Override
    3. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    4. doGet(request, response);
    5. }
    6. @Override
    7. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    8. request.setCharacterEncoding("UTF-8");
    9. response.setContentType("text/html;charset=UTF-8");
    10. //1. 接收请求参数userName和userPwd
    11. String userName = request.getParameter("userName");
    12. String userPwd = request.getParameter("userPwd");
    13. System.out.println(userName + ":" + userPwd);
    14. //模拟出现异常
    15. //int num = 10/0;
    16. //2. 向浏览器响应数据
    17. response.getWriter().write("hello world!!!");
    18. }
    19. }

    ③ 服务器端处理请求失败后

    1. catch(error => { // catch()服务器端处理请求出错后,会调用
    2. console.log(error); // error就是出错时服务器端返回的响应数据
    3. });

    在给catch()函数传入的回调函数中,error对象封装了服务器端处理请求失败后相应的错误信息。其中,axios封装的响应数据对象,是error对象的response属性。response属性对象的结构如下图所示:

    可以看到,response对象的结构还是和then()函数传入的回调函数中的response是一样的:

    2.3 服务器端返回JSON数据

    2.3.1 前端代码

    1. sendJsonBody(){
    2. //使用axios发送异步请求,要携带Json请求体的参数
    3. axios({
    4. "method":"post",
    5. "url":"demo01",
    6. //携带Json请求体参数
    7. "data":{
    8. "userName":"aobama",
    9. "userPwd":"999999"
    10. }
    11. }).then(response => {
    12. //目标是获取响应数据中的用户的用户名或者密码
    13. this.message = response.data.userName
    14. })
    15. }

    2.3.2 后端代码

    ① 加入Gson包

    ② Servlet代码  

    1. public class ServletDemo01 extends HttpServlet {
    2. @Override
    3. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    4. doGet(request, response);
    5. }
    6. @Override
    7. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    8. try {
    9. response.setContentType("text/html;charset=UTF-8");
    10. String userName = request.getParameter("userName");
    11. String userPwd = request.getParameter("userPwd");
    12. //模拟服务器出现异常
    13. //int num = 10/0;
    14. //服务器端向客户端响应普通字符串
    15. //response.getWriter().write("你好世界");
    16. //在实际开发中服务器端向客户端响应的99%都会是Json字符串
    17. User responseUser = new User("周杰棍","ggggggg");
    18. //将responseUser转成json字符串
    19. String responseJson = gson.toJson(responseUser);
    20. response.getWriter().write(responseJson);
    21. } catch (Exception e) {
    22. e.printStackTrace();
    23. }
    24. }
    25. }
  • 相关阅读:
    (附源码课件)10款Java小游戏满足你各种需求
    以32bit加法器为核心的加法、减法、乘法和除法计算器(ALU)
    什么是软件测试?
    【办公类-16-07-04】合并版“2023下学期 中班户外游戏(有场地和无场地版,一周一次)”(python 排班表系列)
    后端 --- Elasticsearch学习笔记(入门篇)
    基于ssm框架的图片分享及评价网站设计与实现毕业设计源码201524
    使用HTML制作静态网站作业——我的校园运动会(HTML+CSS)
    03-Redis 凭什么这么快
    为什么炒股人更爱融资?融券交易背后的风险与获利机会
    公司来了个00后,起薪就是18K,不愧是卷王。。。
  • 原文地址:https://blog.csdn.net/rbx508780/article/details/127670364