Ajax重复请求

在发送某一次请求时,如果不对请求做去重处理那么,同一个请求有可能会发送很多次,一个个慢慢响应,这会很大程度影响性能以及页面的使用体验 全局变量 1 2 3 4 5 6 7 /** * baseURL 是每次请求的地址 * x 表示XMLHttpRequest实例,初始为null * isSending 是用来判断请求是否处于发送状态的标识 */ const baseURL = "http://127.0.0.1:8000/delay" let x = null, isSending = false 原生请求 1 2 3 4 5 6 7 8 9 10 11 12 13 14 if (isSending) x.abort() x = new XMLHttpRequest() //正处于发送状态,修改标识 isSending = true x.open("GET", baseURL) x.send() x.onreadystatechange = function () { if (x....

2020年8月13日 · 2 min · Archai

跨域及CORS官方跨域

JSONP jsonp跨域的实现仅限于GET请求,不可用于POST 说明:实现的基本思路是利用html中script标签本身可跨域的特性,在发送请求时,在页面中创建script标签,追加到页面中。这实际上就像利用script标签引入外部资源 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 /*main.js*/ //申明handle函数 function handle(data) { //do something.... } ele.onclick = function () { //1.创建script标签 const script = document.createElement("script") //2.修改script的src属性 script.src = "http://127.0.0.1:8000/jsonP" script.id="tempScript"//添加id方便移除 //3.追加到页面中 document.body.appendChild(script) } 1 2 3 4 5 6 7 8 9 10 11 12 /*server.js*/ app.get('/jsonP', (request, response) => { const data = { exist:1, msg:"用户名已经存在!...

2020年8月11日 · 1 min · Archai

Ajax学习小结

学习地址:https://www.bilibili.com/video/BV1WC4y1b78y,主要通过express的简单功能来搭建本地的服务,进而更好地学习Ajax 发送Ajax请求的方式 原生Ajax 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 //1.创建对象 const xhr = new XMLHttpRequest() //2.初始化 xhr.open([type], [url])//xhr.open("GET","https://www.x.com") //3.发送 xhr.send() //4.处理返回结果 xhr.onreadystatechange = function () { if (xhr.readyState === 4) { if (xhr.status >= 200 && xhr.status < 300) { //...do something console.log(xhr.status)//状态码 console.log(xhr.statusText)//状态字符串 console.log(xhr.getAllResponseHeaders)//所有请求头信息 console.log(xhr.response)//响应体 } } } 说明:...

2020年8月10日 · 4 min · Archai

Ajax基础及Express框架基本使用

Baike Ajax 即“AsynchronousJavascript And XML”(异步 JavaScript 和 XML),是指一种创建交互式、快速动态网页应用的网页开发技术,无需重新加载整个网页的情况下,能够更新部分网页的技术。 通过在后台与服务器进行少量数据交换,Ajax 可以使网页实现异步更新。这意味着可以在不重新加载整个网页的情况下,对网页的某部分进行更新。—百度百科 请求报文 格式以及参数如下 1 2 3 4 5 6 7 8 行 GET / HTTP/1.1 头 Host: fintinger.xyz Cookie: keyword=xyz Content-type: application/x-www-form-urlencoded User-agent: Chrome 83 空行 (必须有) 体 [GET]:空 [POST]:空/username=admin&password=admin 图示为Chrome浏览器请求报文 响应报文 格式以及参数如下 1 2 3 4 5 6 7 8 9 10 11 12 行 HTTP/1.1 200 Ok 头 Content-Type: text/html; charset=utf-8 Content-Length: 22 Content-encoding: gzip 空行 (必须有) 体 <html> <head> </head> <body> <h1>Hello!...

2020年8月6日 · 1 min · Archai