Ajax学习笔记

(100条消息) Ajax 请求的五大步骤_没有天赋全靠手打的博客-CSDN博客_ajax请求的五个步骤

 连接在同一个局域网

 navicat可以将数据转化为sql语句

 

 四种提交request的方式:

url,超链接,form表单,还有js提交请求

传统方式:提交一下,就会全部刷新一遍

原来提交的时候,如果一提交表单之后,界面就会刷新,用户原来的体验就会断掉

Ajax请求可以只刷新部分的界面,不会刷新整个界面

 可以多个线程,并不会之间相互去影响

异步:类似于多线程并发

 (108条消息) console.log有什么作用_hellowindowns的博客-CSDN博客_console.log()的作用

(108条消息) 常见的HTTP状态码(HTTP Status Code)说明_dufufd的博客-CSDN博客_status_code

前端代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script>
    window.onload=function (){
        var sub = document.getElementById("submit")
        sub.onclick = function (){
            // 构建xhr
            var xhr = new XMLHttpRequest()
            xhr.onreadystatechange = function (){
                //    回调函数会被执行多次,0 -> 1 1->2 2->3 3->4
                //     console.log(xhr.readyState)
                // if (this.readyState==4) {
                //     console.log("结束进程")
                // }
                // console.log(this.status)
                // alert(this.responseText)
                if (this.readyState == 4) {
                    var elementById = document.getElementById("divid");
                    elementById.innerText = this.responseText;
                }
            }
            //第三步开启通道
            xhr.open("GET","/ajax/ajaxrequest1",true)
            // 第四步发送请求
            xhr.send()
        }
    }
</script>

<!--点击按钮之后,发送ajax请求-->
<input type="button" id="submit">
<div id="divid"></div>
</body>
</html>

后端代码 

package com.wsy;

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;
import java.io.PrintWriter;

@WebServlet("/ajaxrequest1")
public class AjaxDemo extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        String s = null;
//        s.toString();
        PrintWriter out = resp.getWriter();
        out.print("!!!!!nihaoa");

    }
}

readystate说的是ajax响应的状态值有五个 0 -1 -2 -3 -4 

而state说的是返回的状态值有200(表示的是成功)404表示的是找不到资源500表示后端代码有一些错误

ajax的核心对象就是XMLHttpreqquest的对象的responsetext响应的可能有多种,但是这里获得都是文本的形式

ajax的四个步骤:

第一步就是注册XMLHttpRequest的对象,下一步就是注册回调函数,第三步是用open函数来开启通道,下一步是调用send来发送请求

ajax提交数据:

最好采用onchange之后直接去写回调函数

get提交数据格式是url?name=value&name=value

获取ajax的请求数据

前端代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script type="text/javascript">
  window.onload = function (){
     document.getElementById("buttons-hbox") .onclick = function (){
         var xhr = new XMLHttpRequest();
         //注册回调函数
         xhr.onreadystatechange = function (){
             if (this.readyState == 4) {
                 if (this.status == 200) {
                     alert(this.responseText)
                         var myspan =document.getElementById("myspan");
                         myspan.innerText = this.responseText;
                         console.log(this.status)
                     document.getElementById("mydiv").innerHTML = xhr.responseText

                 }else{
                     alert(this.status)
                 }
             }
         }
         xhr.open("GET","/ajax/ajaxrequest02?user=wsy&password=lz",true);
         xhr.send();
         // xhr.open("GET","/ajax/ajaxrequest1",true)
         // // 第四步发送请求
         // xhr.send()
     }
  }
</script>
<input type="button" id="buttons-hbox">
<span id="myspan"></span>
<div id="mydiv"></div>
</body>
</html>

后端代码

package com.wsy;

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("/ajaxrequest02")
public class AjaxDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String user = req.getParameter("user");
        String password = req.getParameter("password");
        System.out.println("user="+user+"password="+password);
        resp.getWriter().print("user="+user+"password="+password);
    }
}

自定义的数据的上传、

前端

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<script type="text/javascript">
  window.onload = function (){
     document.getElementById("buttons-hbox") .onclick = function (){
         var xhr = new XMLHttpRequest();
         //注册回调函数
         xhr.onreadystatechange = function (){
             if (this.readyState == 4) {
                 if (this.status == 200) {
                     alert(this.responseText)
                         var myspan =document.getElementById("myspan");
                         myspan.innerText = this.responseText;
                         console.log(this.status)
                     document.getElementById("mydiv").innerHTML = xhr.responseText

                 }else{
                     alert(this.status)
                 }
             }
         }
         xhr.open("GET","/ajax/ajaxrequest02?user="+document.getElementById("username").value+"&password="+document.getElementById("password").value,true);
         xhr.send();
         // xhr.open("GET","/ajax/ajaxrequest1",true)
         // // 第四步发送请求
         // xhr.send()
     }
  }
</script>
username<input type="text"  id="username"><br>
password<input type="password"  id="password"><br>
<input type="button" id="buttons-hbox">
<span id="myspan"></span>
<div id="mydiv"></div>
</body>
</html>

后端

package com.wsy;

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("/ajaxrequest02")
public class AjaxDemo02 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String user = req.getParameter("user");
        String password = req.getParameter("password");
        System.out.println("user="+user+"password="+password);
        resp.getWriter().print("user="+user+"password="+password);
    }
}

 大部分的浏览器都已经不存在Ajax GET的请求缓存问题

GET请求的结果会被缓存起来

第一次加载比较慢。第二次加载出来之后 图片之类的资源直接被缓存在浏览器里面了 post不会被浏览器缓存 路径一模一样就会走缓存 只要路径有所变化 就不会走缓存

优点是:可以直接从浏览器上面拿到资源,速度快

缺点是拿不到实时的资源

浏览器走缓存条件:

第一是一个GET请求第二个是路径没有发生变化

谷歌浏览器就不会走缓存,走缓存之后就不会加载后台的程序

解决办法:给请求的路径加一个时间戳,这个时间戳是随时变化,这样路径就会发生变化,那么就不会走缓存

发送Ajaxpost 请求:

前后端处理的方式不一样就会发出来405的错误,前端使用的是get请求后台使用的是post的请求就会发生这样的错误

 前端

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>采用ajax-post的方法去发送请求</title>
</head>
<body>
<script type="text/javascript">
    window.onload = function (){
        document.getElementById("mybtn").onclick = function (){
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function (){
                if (this.readyState == 4) {
                    if (this.status == 200) {
                        document.getElementById("mybtn").innerText=this.responseText
                    }else {
                        alert(this.status)
                    }
                }
            }
            xhr.open("POST","/ajax/ajaxrequest03",true)
            xhr.send()
        }
    }
</script>
<button id="mybtn">发送post请求</button>
<div id="mydiv"></div>

</body>
</html>

后端

package com.wsy;

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("/ajaxrequest03")
public class AjaxDemo03 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.getWriter().print("hello,world!");
    }
}

post提交数据的方式

放在send函数的小括号当中会自动进行提交

 但是这样是存在一定的问题的 ->后台拿不到数据

<form enctype="application/x-www-form-urlencoded"></form>

通过上面的那句话拿到后面application这个话

不加

xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded")

send里面的数据发送不过去,只有加上这句话才可以

前端代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>采用ajax-post的方法去发送请求</title>
</head>
<body>
<script type="text/javascript">
    window.onload = function (){
        document.getElementById("mybtn").onclick = function (){
            var xhr = new XMLHttpRequest();
            xhr.onreadystatechange = function (){
                if (this.readyState == 4) {
                    if (this.status == 200) {
                        document.getElementById("mydiv").innerText=this.responseText
                    }else {
                        alert(this.status)
                    }
                }
            }
            xhr.open("POST","/ajax/ajaxrequest03",true)
            xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded")
            xhr.send("username="+document.getElementById("username").value+"password="+document.getElementById("password").value)
        }
    }
</script>
USERNAME<input type="text" id="username">
PASSWORD<input type="text" id="password">
<button id="mybtn">发送post请求</button>
<div id="mydiv"></div>
<form enctype="application/x-www-form-urlencoded"></form>
</body>
</html>

后端代码

package com.wsy;

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("/ajaxrequest03")
public class AjaxDemo03 extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("username="+username+"password="+password);
        resp.getWriter().print("username="+username+"password="+password);
    }
}

这里必须还是得有这个&不然还是得出错 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于学习Ajax,我可以为您提供一些学习笔记。以下是一份简要的Ajax学习笔记,希望对您有所帮助: 1. 什么是Ajax? - Ajax全称为Asynchronous JavaScript and XML(异步JavaScript和XML),是一种用于创建交互式Web应用程序的技术。 - 它通过在后台与服务器进行数据交换,实现在不刷新整个页面的情况下更新部分页面内容。 2. Ajax的优点: - 异步处理:可以在后台发送和接收数据,而无需刷新整个页面。 - 提高用户体验:通过部分更新页面内容,可以提供更快的响应时间和更流畅的用户体验。 - 减轻服务器负担:只更新需要的部分内容,减少了不必要的数据传输和服务器负载。 3. Ajax的基本原理: - 使用XMLHttpRequest对象向服务器发送请求,并接收响应。 - 通过JavaScript在前端处理响应数据。 - 更新页面内容,以显示最新的数据。 4. Ajax的核心技术: - XMLHttpRequest对象:用于与服务器进行数据交换。 - JavaScript:用于处理响应数据和更新页面内容。 - XML或JSON:用于传输数据格式。 5. Ajax的使用步骤: - 创建XMLHttpRequest对象。 - 定义请求类型、URL和是否异步。 - 发送请求并接收响应。 - 处理响应数据并更新页面内容。 6. 常见的Ajax框架和库: - jQuery:一个流行的JavaScript库,提供了简化Ajax开发的方法和函数。 - Axios:一个基于Promise的HTTP客户端,用于浏览器和Node.js。 - Fetch API:一种用于发送和接收网络请求的新标准。 这只是Ajax学习的一些基本概念和步骤,您可以进一步深入学习Ajax的相关知识和实践。希望这些笔记对您有所帮助!如有更多问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值