Ajax

Ajax

异步javascrip 和xml
实现方式:

​ jQuery的ajax方式

原生实现:

四个步骤

1.创建XmlHttpRequest对象

var xmlHttpRequest = new XMLHttpRequest();

var value = document.getElementById("name").value;

2.使用XmlHTttpRequest对象的open(method, url, asyn)方法
method:代表请求方式
url代表要给后端发送什么样请求
asyn是否以异步的方式执行

xmlHttpRequest.open("GET", "LoginServlet?name=" + value, true);

3.使用XmlHttpRequest对象的send()方法来发送请求

xmlHttpRequest.send();

4.使用XmlHttpRequest对象的onreadystatechange时间来执行后期的回调

xmlHttpRequest.onreadystatechange = function () {

        if (xmlHttpRequest.readyState == 4 && xmlHttpRequest.status == 200) {
            
                    // alert(xmlHttpRequest.responseText);
                    var span = document.getElementById("span");

                    if (xmlHttpRequest.responseText == "1") {
                        // alert("用户名已存在");
                        /*
                        xmlHttpRequest的responseText可以获取后端发送的字符串
						xmlHttpRequest的responseXML可以获取后端发送回的xml格式的数据
                        */
                        span.innerText = "用户名已存在";
                    } else if (xmlHttpRequest.responseText == "0") {
                        // alert("用户名可用");
                        span.innerText = "用户名可用";
                    }
                }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值