Sign in
Sign up
Explore
Enterprise
Education
Search
Help
Terms of use
About Us
Explore
Enterprise
Education
Gitee Premium
Gitee AI
AI teammates
Sign in
Sign up
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Cancel
Sign in
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
Watch
Unwatch
Watching
Releases Only
Ignoring
3
Star
47
Fork
21
DreamCoders
/
CoderGuide
Code
Issues
1169
Pull Requests
0
Wiki
Insights
Pipelines
Service
JavaDoc
PHPDoc
Quality Analysis
Jenkins for Gitee
Tencent CloudBase
Tencent Cloud Serverless
悬镜安全
Aliyun SAE
Codeblitz
SBOM
Don’t show this again
Update failed. Please try again later!
Remove this flag
Content Risk Flag
This task is identified by
as the content contains sensitive information such as code security bugs, privacy leaks, etc., so it is only accessible to contributors of this repository.
说说ajax的原理,以及如何实现?
Backlog
#IAG9PG
陌生人
owner
Opened this issue
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>
Comments (
0
)
Sign in
to comment
Status
Backlog
Backlog
Doing
Done
Closed
Assignees
Not set
Labels
Html/JS/CSS
Not set
Label settings
Milestones
No related milestones
No related milestones
Pull Requests
None yet
None yet
Successfully merging a pull request will close this issue.
Branches
No related branch
No related branch
master
Planed to start   -   Planed to end
-
Top level
Not Top
Top Level: High
Top Level: Medium
Top Level: Low
Priority
Not specified
Serious
Main
Secondary
Unimportant
参与者(1)
1
https://gitee.com/DreamCoders/CoderGuide.git
git@gitee.com:DreamCoders/CoderGuide.git
DreamCoders
CoderGuide
CoderGuide
Going to Help Center
Search
Git 命令在线学习
如何在 Gitee 导入 GitHub 仓库
Git 仓库基础操作
企业版和社区版功能对比
SSH 公钥设置
如何处理代码冲突
仓库体积过大,如何减小?
如何找回被删除的仓库数据
Gitee 产品配额说明
GitHub仓库快速导入Gitee及同步更新
什么是 Release(发行版)
将 PHP 项目自动发布到 packagist.org
Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register