分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 技术分享

ajax中的onload和readychange区别

发布时间:2023-09-06 01:46责任编辑:沈小雨关键词:暂无标签

先补个知识点:

readyState 状态码:

0:请求未初始化

1:服务器连接已建立

2:请求已接受

3:请求处理中

4:请求已完成,且响应已就绪

HTTP 状态码:

200 - 服务器成功返回网页

404 - 请求的网页不存在

503 - 服务器暂时不可用

首先在自己目录下建立一个ajaxText.txt用来测试,ajax必须要服务器里面执行,我当前是在apach本地服务器测试的。

<!DOCTYPE html><html> ???<head> ???????<meta charset="UTF-8"> ???????<title></title> ???</head><button id="btn" value="请求数据">请求数据</button><p id="c"></p><body> ???????<!-- open(type, url/file,async) --> ???????<script type="text/javascript"> ???????????????????????????????????let btnRequest = document.getElementById(‘btn‘); ???????????btnRequest.addEventListener(‘click‘, load, false); ???????????????????????function load() { ???????????let xhr = new XMLHttpRequest(); ???????????xhr.open(‘GET‘, ‘ajaxTest.txt‘,true); ???????????//两种方式请求 onload / onreadystatechange ???????????????????????xhr.onload = function(){ ???????????????console.log(`onload 状态码${xhr.readyState}`); ???????????????console.log(`这是onload函数请求的文本:${this.responseText}`); ???????????} ???????????//当state状态发生改变时回调一次后面的匿名函数 ???????????xhr.onreadystatechange = function(){ ???????????????console.log(`onreadystatechange 状态码${xhr.readyState}`); ???????????????console.log(`这是onreadychange函数请求的文本:${this.responseText}`); ???????????} ???????????xhr.send(); ???????????} ???????</script> ???</body></html>

console:

onreadystatechange()的定义是只要返回的状态码只要变化时就回调一次函数,而onload只有状态码为4时才能回调一次函数。

这边提下onprogress(),也就是当状态码为3时,会执行这个函数。

当服务器正常的话基本上都会返回readyState 状态码0~4,但是不一定请求得到数据,所以有个http状态码来判断。

 ???????????xhr.onreadystatechange = function(){ ???????????????if (xhr.readyState == 4 && xhr.status == 200) ???????????????{ ???????????????????console.log(`请求成功并返回数据${this.responseText}`); ???????????????} ???????????}

在onload()里面也是一样,这里的逻辑根据情况来写。

<!DOCTYPE html><html> ???<head> ???????<meta charset="UTF-8"> ???????<title></title> ???</head><button id="btn">请求数据</button><p id="c"></p><body> ???????<!-- open(type, url/file,async) --> ???????<script type="text/javascript"> ???????????????????????????????????let btnRequest = document.getElementById(‘btn‘); ???????????btnRequest.addEventListener(‘click‘, load, false); ???????????????????????function load() { ???????????let xhr = new XMLHttpRequest(); ???????????xhr.open(‘GET‘, ‘ajaxTest.txt‘,true); ???????????//两种方式请求 onload / onreadystatechange ???????????????????????// ???????????xhr.onload = function(){// ???????????????if (xhr.status == 200)// ???????????????{// ???????????????????console.log(`请求成功并返回数据${this.responseText}`);// ???????????????}// ???????????????else{// ???????????????????console.log(`请求不成功`);// ???????????????}//// ???????????????console.log(`onload 状态码${xhr.readyState}`);//// ???????????????console.log(`这是onload函数请求的文本:${this.responseText}`);// ???????????} ???????????//当state状态发生改变时回调一次后面的匿名函数 ???????????xhr.onreadystatechange = function(){ ???????????????if (xhr.readyState == 4 && xhr.status == 200) ???????????????{ ???????????????????let p = document.createElement("p"); ???????????????????p.innerHTML = this.responseText; ???????????????????document.body.appendChild(p); ???????????????????console.log(`请求成功并返回数据${this.responseText}`); ???????????????} ???????????} ???????????xhr.send(); ???????????} ???????</script> ???</body></html>

ajax中的onload和readychange区别

原文地址:https://www.cnblogs.com/doudoublog/p/8608360.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved