登录
注册
开源
企业版
高校版
搜索
帮助中心
使用条款
关于我们
开源
企业版
高校版
私有云
模力方舟
AI 队友
登录
注册
3月21日 深圳|OpenClaw 线下实战沙龙:招聘、资讯、项目协同三大场景实操,VS ZeroClaw 横向对比评测,别再只会装,来现场跑通真实业务!
代码拉取完成,页面将自动刷新
捐赠
捐赠前请先登录
取消
前往登录
扫描微信二维码支付
取消
支付完成
支付提示
将跳转至支付宝完成支付
确定
取消
Watch
不关注
关注所有动态
仅关注版本发行动态
关注但不提醒动态
3
Star
47
Fork
21
DreamCoders
/
CoderGuide
代码
Issues
1169
Pull Requests
0
Wiki
统计
流水线
服务
JavaDoc
PHPDoc
质量分析
Jenkins for Gitee
腾讯云托管
腾讯云 Serverless
悬镜安全
阿里云 SAE
Codeblitz
SBOM
我知道了,不再自动展开
更新失败,请稍后重试!
移除标识
内容风险标识
本任务被
标识为内容中包含有代码安全 Bug 、隐私泄露等敏感信息,仓库外成员不可访问
说说ajax的原理,以及如何实现?
待办的
#IAG9PG
陌生人
拥有者
创建于
2024-07-29 16:07
<h2>一、是什么</h2><p><code>AJAX</code>全称(Async Javascript and XML)</p><p>即异步的<code>JavaScript</code> 和<code>XML</code>,是一种创建交互式网页应用的网页开发技术,可以在不重新加载整个网页的情况下,与服务器交换数据,并且更新部分网页</p><p><code>Ajax</code>的原理简单来说通过<code>XmlHttpRequest</code>对象来向服务器发异步请求,从服务器获得数据,然后用<code>JavaScript</code>来操作<code>DOM</code>而更新页面</p><p>流程图如下:</p><p><img src="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/c0bc6217-f28c-450a-a45b-154c8a31bb55.png" alt="" data-href="" style=""/></p><p>下面举个例子:</p><p>领导想找小李汇报一下工作,就委托秘书去叫小李,自己就接着做其他事情,直到秘书告诉他小李已经到了,最后小李跟领导汇报工作</p><p><code>Ajax</code>请求数据流程与“领导想找小李汇报一下工作”类似,上述秘书就相当于<code>XMLHttpRequest</code>对象,领导相当于浏览器,响应数据相当于小李</p><p>浏览器可以发送<code>HTTP</code>请求后,接着做其他事情,等收到<code>XHR</code>返回来的数据再进行操作</p><h2>二、实现过程</h2><p>实现 <code>Ajax</code>异步交互需要服务器逻辑进行配合,需要完成以下步骤:</p><ul><li>创建 Ajax的核心对象 XMLHttpRequest对象</li><li>通过 XMLHttpRequest 对象的 open() 方法与服务端建立连接</li><li>构建请求所需的数据内容,并通过XMLHttpRequest 对象的 send() 方法发送给服务器端</li><li>通过 XMLHttpRequest 对象提供的 onreadystatechange 事件监听服务器端你的通信状态</li><li>接受并处理服务端向客户端响应的数据结果</li><li>将处理结果更新到 HTML页面中</li></ul><h3>创建XMLHttpRequest对象</h3><p>通过<code>XMLHttpRequest()</code> 构造函数用于初始化一个 <code>XMLHttpRequest</code> 实例对象</p><pre><code class="language-js">const xhr = new XMLHttpRequest();</code></pre><h3>与服务器建立连接</h3><p>通过 <code>XMLHttpRequest</code> 对象的 <code>open()</code> 方法与服务器建立连接</p><pre><code class="language-js">xhr.open(method, url, [async][, user][, password])</code></pre><p>参数说明:</p><ul><li>method:表示当前的请求方式,常见的有GET、POST</li><li>url:服务端地址</li><li>async:布尔值,表示是否异步执行操作,默认为true</li><li>user: 可选的用户名用于认证用途;默认为`null</li><li>password: 可选的密码用于认证用途,默认为`null</li></ul><h3>给服务端发送数据</h3><p>通过 <code>XMLHttpRequest</code> 对象的 <code>send()</code> 方法,将客户端页面的数据发送给服务端</p><pre><code class="language-js">xhr.send([body])</code></pre><p><code>body</code>: 在 <code>XHR</code> 请求中要发送的数据体,如果不传递数据则为 <code>null</code></p><p>如果使用<code>GET</code>请求发送数据的时候,需要注意如下:</p><ul><li>将请求数据添加到<code>open()</code>方法中的<code>url</code>地址中</li><li>发送请求数据中的<code>send()</code>方法中参数设置为<code>null</code></li></ul><h3>绑定onreadystatechange事件</h3><p><code>onreadystatechange</code> 事件用于监听服务器端的通信状态,主要监听的属性为<code>XMLHttpRequest.readyState</code> ,</p><p>关于<code>XMLHttpRequest.readyState</code>属性有五个状态,如下图显示</p><p><img src="https://static.ecool.fun//article/bd7bf527-914d-4195-82a0-d0f1ea28e00a.png" alt="" data-href="" style=""/></p><p>只要 <code>readyState</code>属性值一变化,就会触发一次 <code>readystatechange</code> 事件</p><p><code>XMLHttpRequest.responseText</code>属性用于接收服务器端的响应结果</p><p>举个例子:</p><pre><code class="language-js">const request = new XMLHttpRequest() request.onreadystatechange = function(e){ if(request.readyState === 4){ // 整个请求过程完毕 if(request.status >= 200 && request.status <= 300){ console.log(request.responseText) // 服务端返回的结果 }else if(request.status >=400){ console.log("错误信息:" + request.status) } } } request.open('POST','http://xxxx') request.send()</code></pre><h2>三、封装</h2><p>通过上面对<code>XMLHttpRequest</code>对象的了解,下面来封装一个简单的<code>ajax</code>请求</p><pre><code class="language-js">//封装一个ajax请求 function ajax(options) { //创建XMLHttpRequest对象 const xhr = new XMLHttpRequest() //初始化参数的内容 options = options || {} options.type = (options.type || 'GET').toUpperCase() options.dataType = options.dataType || 'json' const params = options.data //发送请求 if (options.type === 'GET') { xhr.open('GET', options.url + '?' + params, true) xhr.send(null) } else if (options.type === 'POST') { xhr.open('POST', options.url, true) xhr.send(params) //接收请求 xhr.onreadystatechange = function () { if (xhr.readyState === 4) { let status = xhr.status if (status >= 200 && status < 300) { options.success && options.success(xhr.responseText, xhr.responseXML) } else { options.fail && options.fail(status) } } } }</code></pre><p>使用方式如下</p><pre><code class="language-js">ajax({ type: 'post', dataType: 'json', data: {}, url: 'https://xxxx', success: function(text,xml){//请求成功后的回调函数 console.log(text) }, fail: function(status){////请求失败后的回调函数 console.log(status) } })</code></pre><p><br></p>
<h2>一、是什么</h2><p><code>AJAX</code>全称(Async Javascript and XML)</p><p>即异步的<code>JavaScript</code> 和<code>XML</code>,是一种创建交互式网页应用的网页开发技术,可以在不重新加载整个网页的情况下,与服务器交换数据,并且更新部分网页</p><p><code>Ajax</code>的原理简单来说通过<code>XmlHttpRequest</code>对象来向服务器发异步请求,从服务器获得数据,然后用<code>JavaScript</code>来操作<code>DOM</code>而更新页面</p><p>流程图如下:</p><p><img src="https://jsd.onmicrosoft.cn/gh/iGaoWei/codercdn@master/question/20240627/c0bc6217-f28c-450a-a45b-154c8a31bb55.png" alt="" data-href="" style=""/></p><p>下面举个例子:</p><p>领导想找小李汇报一下工作,就委托秘书去叫小李,自己就接着做其他事情,直到秘书告诉他小李已经到了,最后小李跟领导汇报工作</p><p><code>Ajax</code>请求数据流程与“领导想找小李汇报一下工作”类似,上述秘书就相当于<code>XMLHttpRequest</code>对象,领导相当于浏览器,响应数据相当于小李</p><p>浏览器可以发送<code>HTTP</code>请求后,接着做其他事情,等收到<code>XHR</code>返回来的数据再进行操作</p><h2>二、实现过程</h2><p>实现 <code>Ajax</code>异步交互需要服务器逻辑进行配合,需要完成以下步骤:</p><ul><li>创建 Ajax的核心对象 XMLHttpRequest对象</li><li>通过 XMLHttpRequest 对象的 open() 方法与服务端建立连接</li><li>构建请求所需的数据内容,并通过XMLHttpRequest 对象的 send() 方法发送给服务器端</li><li>通过 XMLHttpRequest 对象提供的 onreadystatechange 事件监听服务器端你的通信状态</li><li>接受并处理服务端向客户端响应的数据结果</li><li>将处理结果更新到 HTML页面中</li></ul><h3>创建XMLHttpRequest对象</h3><p>通过<code>XMLHttpRequest()</code> 构造函数用于初始化一个 <code>XMLHttpRequest</code> 实例对象</p><pre><code class="language-js">const xhr = new XMLHttpRequest();</code></pre><h3>与服务器建立连接</h3><p>通过 <code>XMLHttpRequest</code> 对象的 <code>open()</code> 方法与服务器建立连接</p><pre><code class="language-js">xhr.open(method, url, [async][, user][, password])</code></pre><p>参数说明:</p><ul><li>method:表示当前的请求方式,常见的有GET、POST</li><li>url:服务端地址</li><li>async:布尔值,表示是否异步执行操作,默认为true</li><li>user: 可选的用户名用于认证用途;默认为`null</li><li>password: 可选的密码用于认证用途,默认为`null</li></ul><h3>给服务端发送数据</h3><p>通过 <code>XMLHttpRequest</code> 对象的 <code>send()</code> 方法,将客户端页面的数据发送给服务端</p><pre><code class="language-js">xhr.send([body])</code></pre><p><code>body</code>: 在 <code>XHR</code> 请求中要发送的数据体,如果不传递数据则为 <code>null</code></p><p>如果使用<code>GET</code>请求发送数据的时候,需要注意如下:</p><ul><li>将请求数据添加到<code>open()</code>方法中的<code>url</code>地址中</li><li>发送请求数据中的<code>send()</code>方法中参数设置为<code>null</code></li></ul><h3>绑定onreadystatechange事件</h3><p><code>onreadystatechange</code> 事件用于监听服务器端的通信状态,主要监听的属性为<code>XMLHttpRequest.readyState</code> ,</p><p>关于<code>XMLHttpRequest.readyState</code>属性有五个状态,如下图显示</p><p><img src="https://static.ecool.fun//article/bd7bf527-914d-4195-82a0-d0f1ea28e00a.png" alt="" data-href="" style=""/></p><p>只要 <code>readyState</code>属性值一变化,就会触发一次 <code>readystatechange</code> 事件</p><p><code>XMLHttpRequest.responseText</code>属性用于接收服务器端的响应结果</p><p>举个例子:</p><pre><code class="language-js">const request = new XMLHttpRequest() request.onreadystatechange = function(e){ if(request.readyState === 4){ // 整个请求过程完毕 if(request.status >= 200 && request.status <= 300){ console.log(request.responseText) // 服务端返回的结果 }else if(request.status >=400){ console.log("错误信息:" + request.status) } } } request.open('POST','http://xxxx') request.send()</code></pre><h2>三、封装</h2><p>通过上面对<code>XMLHttpRequest</code>对象的了解,下面来封装一个简单的<code>ajax</code>请求</p><pre><code class="language-js">//封装一个ajax请求 function ajax(options) { //创建XMLHttpRequest对象 const xhr = new XMLHttpRequest() //初始化参数的内容 options = options || {} options.type = (options.type || 'GET').toUpperCase() options.dataType = options.dataType || 'json' const params = options.data //发送请求 if (options.type === 'GET') { xhr.open('GET', options.url + '?' + params, true) xhr.send(null) } else if (options.type === 'POST') { xhr.open('POST', options.url, true) xhr.send(params) //接收请求 xhr.onreadystatechange = function () { if (xhr.readyState === 4) { let status = xhr.status if (status >= 200 && status < 300) { options.success && options.success(xhr.responseText, xhr.responseXML) } else { options.fail && options.fail(status) } } } }</code></pre><p>使用方式如下</p><pre><code class="language-js">ajax({ type: 'post', dataType: 'json', data: {}, url: 'https://xxxx', success: function(text,xml){//请求成功后的回调函数 console.log(text) }, fail: function(status){////请求失败后的回调函数 console.log(status) } })</code></pre><p><br></p>
评论 (
0
)
登录
后才可以发表评论
状态
待办的
待办的
进行中
已完成
已关闭
负责人
未设置
标签
Html/JS/CSS
未设置
标签管理
里程碑
未关联里程碑
未关联里程碑
Pull Requests
未关联
未关联
关联的 Pull Requests 被合并后可能会关闭此 issue
分支
未关联
未关联
master
开始日期   -   截止日期
-
置顶选项
不置顶
置顶等级:高
置顶等级:中
置顶等级:低
优先级
不指定
严重
主要
次要
不重要
参与者(1)
1
https://gitee.com/DreamCoders/CoderGuide.git
git@gitee.com:DreamCoders/CoderGuide.git
DreamCoders
CoderGuide
CoderGuide
点此查找更多帮助
搜索帮助
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
评论
仓库举报
回到顶部
登录提示
该操作需登录 Gitee 帐号,请先登录后再操作。
立即登录
没有帐号,去注册