AJAX

作者: 蛋炒饭_By | 来源:发表于2018-03-28 16:42 被阅读2次

Ajax

AJAX:即“Asynchronous Javascript And XML”(异步的JavaScript和XML),是指一种创建交互式网页应用的网页开发技术,尤其是在一种在无需重新加载整个网页的情况下,能够更新部分网页的技术。

传统Web开发

World Wide Web(简称Web):是随着Internet的普及使用而发展起来的一门技术,其开发模式是一种请求→刷新→响应的模式,每个请求由单独的一个页面来显示,发送一个请求就会重新获取这个页面。

image

Ajax采用异步通信,主要以数据交互为主;传统的web开发采用同步通信,主要以页面交互为主。

ajax请求步骤

1.创建Ajax对象

var request = new XMLHttpRequest();

2.连接服务器
open(method,url,async);

request.open("get","query.do",true);//异步请求

3.发送请求
send(string)
在使用GET方式请求时无需填写参数
在使用POST方式时参数代表着向服务器发送的数据

        xhr.open('get','random.do?max=100‘,true);
        xhr.send();

//        xhr.open('post','random.do',true);
//        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");//post请求需要设置HTTP头信息,否则发送数据有问题
//        xhr.send('max=100');

4.接收服务器相应数据

 xhr.onload = function () {
          console.log(xhr.responseText);
        }

一个综合实例

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title></title>
    <script>
      function getRandom() {
        var max = document.getElementById('max');

        var xhr = new XMLHttpRequest();

        xhr.open('get','random.do?max='+ max.value,true);
        xhr.send();

//        xhr.open('post','random.do',true);
//        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
//        xhr.send('max='+ max.value);
        xhr.onload = function () {
          var div = document.getElementById('num');
          div.innerHTML = xhr.responseText;
        }
      }

    </script>
  </head>
  <body>
  多少以内的随机数:<input type="text" id="max">
  <button onclick="getRandom();">得到一个随机数</button>
  <div id="num">

  </div>

  </body>
</html>

@WebServlet(name = "RandomServlet",urlPatterns = "/random.do")
public class RandomServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int max = Integer.parseInt(request.getParameter("max"));
        Random random = new Random();
        int num = random.nextInt(max);
        response.getWriter().println(num);
    }
}

可以写一个传统web开发方式的请求回应,进行对比。

ajax校验用户名是否已存在

image
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>$Title$</title>
  <script>
    function checkUser() {
      //创建一个XMLHttpRequest类型的对象ajaxReq
      var ajaxReq = new XMLHttpRequest();
      var username = document.getElementById('username');

      //用ajaxReq打开一个连接
      ajaxReq.open("post","valid.do",true);

      ajaxReq.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      //发送请求给服务器
      ajaxReq.send("username="+username.value);
      //设置一个回调函数,用来处理服务器的回应。
      ajaxReq.onload = function () {
            var msg = document.getElementById('msg');
            if(ajaxReq.responseText=="0")//可以注册,用户名还不存在
            {
              msg.innerHTML="可以注册";
            }
            else
            {
              msg.innerHTML="用户名已存在";
            }
      }
    }
  </script>
</head>
<body>
用户注册
<form method="post" action="valid.do">
  用户名:<input type="text" id = "username" name="username" onblur="checkUser();">
  <span id="msg"></span>
  密码:<input type="text" name="pwd">
  <input type="submit">
</form>
</body>
</html>

ValidUserServlet.java

@WebServlet(name = "ValidUserServlet",urlPatterns = "/valid.do")
public class ValidUserServlet extends HttpServlet {
    private List<String> lst = new ArrayList<String>();
    public void init() throws javax.servlet.ServletException
    { /* compiled code */
        lst.add("zhangsan");
        lst.add("lisi");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String strName = request.getParameter("username");
        //contains---包含
        PrintWriter pw = response.getWriter();
        if(lst.contains(strName))//用户已存在
        {
            pw.print("1");
        }
        else
        {
            pw.print("0");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}

相关文章

  • AJAX

    主要内容: ajax 是什么、原生ajax 写法和jQuery ajax写法。 AJAX 是什么 ajax,即As...

  • JavaScript进阶知识点--AJAX及JSON

    AJAX 关于 AJAX 什么是 AJAX AJAX 的全称是 Asynchronous JavaScript a...

  • HTML5权威指南 | 第五部分 高级功能

    三十二、使用AJAX(上) Ajax起步: 使用Ajax事件: Ajax请求的错误处理: 中止Ajax请求: 三十...

  • ajax学习笔记

    Ajax学习笔记 Ajax简介 1. Ajax是什么? Ajax : Asynochronous javascri...

  • AJAX

    一、简介 AJAX菜鸟教程 什么是 AJAX ? AJAX = 异步 JavaScript 和 XML。 AJAX...

  • js之AJAX复习

    异步交互和同步交互 什么是Ajax? Ajax的工作原理。 Ajax包含的技术: Ajax的缺陷: Ajax的核心...

  • 复习jQuery - ajax

    jQuery ajax - ajax() 方法 $.ajax({ url:'oo.php', ...

  • jQuery中Ajax请求的使用和四个步骤示例

    ajax() 方法用于执行 AJAX(异步 HTTP)请求,所有的 jQuery AJAX 方法都使用 ajax(...

  • ajax

    1、什么是ajax? 2、ajax的原理 3、ajax的核心对象 4、ajax的优点: ajax的缺点: 被jqu...

  • ajax

    Ajax 1 - 请求纯文本 Ajax 2 - 请求JSON数据 Ajax 3 - 请求Github接口 Ajax...

网友评论

      本文标题:AJAX

      本文链接:https://www.haomeiwen.com/subject/hcescftx.html