原生ajax请求与jquery-ajax请求

约定不等于承诺〃 2022-02-04 08:29 539阅读 0赞

ajax请求可以在不刷新页面的情况下,请求后台服务器
为了演示的效果,在jsp中添加一段视频,再播放视频的时候请求后台,看效果。

  • 普通表单提交返回数据,会刷新页面使得视频会又从头播放

jsp中

  1. <%@ page language="java" contentType="text/html; charset=UTF-8"
  2. pageEncoding="UTF-8"%>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  4. <html>
  5. <head>
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  7. <title>Insert title here</title>
  8. </head>
  9. <body>
  10. <video src="videos/2.MP4" controls width="500px"></video>
  11. <form action="CommentServlet" method="post">
  12. <input type="text" name="comment" id="c"/>
  13. <input type="submit" value="表单提交">
  14. </form>
  15. <div id="result">
  16. 表单提交返回数据:${comment}
  17. </div>
  18. </body>
  19. </html>

servlet中

  1. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  2. String string = request.getParameter("comment");
  3. System.out.println("comment:"+string);
  4. request.setAttribute("comment", string);
  5. request.getRequestDispatcher("ajax.jsp").forward(request, response);
  6. }

  • 利用XMLHttpRequest()对象,进行ajax请求,不会刷新页面
    jsp中

    <%@ page language=”java” contentType=”text/html; charset=UTF-8”

    1. pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd">



    Insert title here



    1. <input type="text" name="comment" id="c"/>
    2. <input type="button" onclick="getAjax()" value="ajax提交">




注:
true: 默认值,异步请求,send方法不会阻塞,其他代码正常执行,异步请求处理响应得用到事件,响应返回时触发此事件,执行对应的函数 xhr.οnlοad=function(){xhr.responseTest;}
最早处理响应返回事件的是xhr.onreadstatechange=function(){xhr.readyState}
发送请求时触发,响应返回时会触发,响应完全返回会触发
状态值变化
xhr 创建时 0
xhr.send 0–>1
xhr. 1–>2 2–>3 (服务器处理时)
3–>4时候表示响应完全返回 ,只需要判断如下就行
if(xhr.readyState==4){}

false: 同步请求 响应没有返回之前,页面代码会暂停,send方法在此期间阻塞。

post请求和get

  1. get: 请求只有请求行和请求头
  2. post: 请求行,请求头,请求体
  3. xhr.open("post",url,true);
  4. xhr.setRequestHeader("content-type","application/x-www-form-urlencoded");//告诉服务器请求体格式为表单格式
  5. xhr.send("请求体内容")//参数名1=参数值&参数名2=参数值

servlet中,直接response回应就好。

  1. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  2. String string = request.getParameter("comment");
  3. System.out.println("comment:"+string);
  4. response.getWriter().print(string);
  5. }

在这里插入图片描述


  • jquery中ajax请求

    <%@ page language=”java” contentType=”text/html; charset=UTF-8”

    1. pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd">



    Insert title here

servlet中

  1. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  2. String string = request.getParameter("comment");
  3. System.out.println("comment:"+string);
  4. response.getWriter().print(string);
  5. }

  • jquery中简写jsp

    <%@ page language=”java” contentType=”text/html; charset=UTF-8”

    1. pageEncoding="UTF-8"%>

    <!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd">



    Insert title here


谢谢!

发表评论

表情:
评论列表 (有 0 条评论,539人围观)

还没有评论,来说两句吧...

相关阅读

    相关 原生JS Ajax请求

    传统方法的缺点: 传统的web交互是用户触发一个http请求服务器,然后服务器收到之后,在做出响应到用户,并且返回一个新的页面,每当服务器处理客户端提交的请求时,客户都只能

    相关 php下的原生ajax请求

    浏览器中为我们提供了一个JS对象XMLHttpRequet,它可以帮助我们发送HTTP请求,并接受服务端的响应。 意味着我们的浏览器不提交,通过JS就可以请求服务器。 ...