From 658edad4ff052e70e7336b0e5ca12b15de58d0f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E7=9D=BF?= <2476310189@qq.com> Date: Wed, 30 Nov 2022 23:09:19 +0000 Subject: [PATCH 1/5] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=2006=E4=BD=95?= =?UTF-8?q?=E7=9D=BF/=E7=AC=94=E8=AE=B0/2022-11-30=20ajax=20=E4=B8=BA=2006?= =?UTF-8?q?=E4=BD=95=E7=9D=BF/=E7=AC=94=E8=AE=B0/2022-11-29=20ajax?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\347\254\224\350\256\260/2022-11-29 ajax/.keep" | 0 .../2022-11-29 ajax/2022-11-30 \347\254\224\350\256\260.md" | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename "06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-30 ajax/.keep" => "06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-29 ajax/.keep" (100%) rename "06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-30 ajax/2022-11-30 \347\254\224\350\256\260.md" => "06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-29 ajax/2022-11-30 \347\254\224\350\256\260.md" (100%) diff --git "a/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-30 ajax/.keep" "b/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-29 ajax/.keep" similarity index 100% rename from "06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-30 ajax/.keep" rename to "06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-29 ajax/.keep" diff --git "a/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-30 ajax/2022-11-30 \347\254\224\350\256\260.md" "b/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-29 ajax/2022-11-30 \347\254\224\350\256\260.md" similarity index 100% rename from "06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-30 ajax/2022-11-30 \347\254\224\350\256\260.md" rename to "06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-29 ajax/2022-11-30 \347\254\224\350\256\260.md" -- Gitee From d440a959154cccd7ca5b2b7b248c19d702c3d2dd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E7=9D=BF?= <2476310189@qq.com> Date: Wed, 30 Nov 2022 23:10:35 +0000 Subject: [PATCH 2/5] =?UTF-8?q?=E6=96=B0=E5=BB=BA=202022-11-30=20=E5=8E=9F?= =?UTF-8?q?=E7=94=9Fajax=E4=B8=8EjQueryAjax?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-30 \345\216\237\347\224\237ajax\344\270\216jQueryAjax/.keep" diff --git "a/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-30 \345\216\237\347\224\237ajax\344\270\216jQueryAjax/.keep" "b/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-30 \345\216\237\347\224\237ajax\344\270\216jQueryAjax/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From 3ba8cfcd2d41416a07e5c0ec44c9d801010a5205 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E7=9D=BF?= <2476310189@qq.com> Date: Wed, 30 Nov 2022 23:11:14 +0000 Subject: [PATCH 3/5] =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 何睿 <2476310189@qq.com> --- .../2022-11-30 \347\254\224\350\256\260.md" | 142 ++++++++++++++++++ 1 file changed, 142 insertions(+) create mode 100644 "06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-30 \345\216\237\347\224\237ajax\344\270\216jQueryAjax/2022-11-30 \347\254\224\350\256\260.md" diff --git "a/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-30 \345\216\237\347\224\237ajax\344\270\216jQueryAjax/2022-11-30 \347\254\224\350\256\260.md" "b/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-30 \345\216\237\347\224\237ajax\344\270\216jQueryAjax/2022-11-30 \347\254\224\350\256\260.md" new file mode 100644 index 0000000..87d3f33 --- /dev/null +++ "b/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-11-30 \345\216\237\347\224\237ajax\344\270\216jQueryAjax/2022-11-30 \347\254\224\350\256\260.md" @@ -0,0 +1,142 @@ +### 一,原生AJAX + +#### 1,GET方法;传送数据量小,处理效率高,安全性低,会被缓存 + +* send()中不需要添加任何参数,因为在连接服务器的时候就已经发送 +* get请求就不必要设置 xhr.setRequestHeader(header,value) + +```js +var newAj=new XMLHttpRequest(); + newAj.open("GET","json练习.json",true); + newAj.send(); + newAj.onreadystatechange=function(){ + if (newAj.readyState==4) { + if (newAj.status==200) { + var obj=JSON.parse(newAj.responseText); + console.log(obj); + } + } + } +``` + +#### 2,post方法;传送数据量大,处理效率低,安全性高,不会被缓存 + +* send()中需要添加参数,建议设置请求头 +* 参数可以是null或者xhr.send()|send(带有参数的)post请求在传递值的情况下必须 设置 xhr.setRequestHeader(header,value) + +```js + var xhr = new XMLHttpRequest(); + xhr.open('post', 'data.json', true); + xhr.send({ a: 100, b: 200 }); + //设置行头 + xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); + xhr.onreadystatechange = function () { + if (xhr.readyState == 4 && xhr.status == 200) { + console.log(xhr.response['No1']); + // 手动转成对象 + // var obj = JSON.parse(xhr.response); + } + } +``` + + + +#### 3.响应数据处理;将返回的json类型的数据转换成对象 + +##### 1,手动转换 + +```js +var obj = JSON.parse(xhr.response) +console.log(obj.name) +``` + +##### 2,自动转换 + +```js +//创建对象后设置: +xhr.responseType = 'json'; +console.log(xhr.response.name) +``` + + + + + +### 二,jQuery Ajax + +#### 1,$.get();执行get请求方式的ajax + +```js + $('button').eq(0).click( + function () { + $.get('data.json',{a:100,b:200},function (data) { + console.log(data); + },'json'); + } + ) +``` + + + +#### 2,$.post();执行post请求方式的ajax + +```js + $('button').eq(1).click( + function () { + $.post('data.json',{a:100,b:200},function (data) { + console.log(data); + },'json'); + } + ) +``` + + + +#### 3,$.getJSON() + +* 通过Ajax获取服务器中JSON格式的数据 +* “全局方法”,直接定义在jQuery对象(即“$”)下的方法 + +```js + $('button').eq(2).click( + function () { + $.getJSON('data.json',function (data) { + console.log(data); + }) + } + ) +``` + + + +#### 4,$.getScript();动态加载JavaScript的方法 + +* 通过AJAX来获取并运行一个外部JavaScript文件 +* 异步跨域加载JavaScript文件,可避免提前加载JavaScript文件 +* 减少服务器和客户端的负 担,加快页面加载速度 + +```js + $('button').eq(3).click( + function () { + $.getScript('test.js'); + } + ) +``` + +#### 5,$.ajax();核心方法,所有的其他方法都是在内部使用此方法 + +* 以上方法用ajax()都能实现 + +```js + $('button').eq(4).click( + function () { + $.ajax({ + url:'data.json', + type:'get', + data:{a:100,b:200}, + success:(data) => console.log(data), + error:()=> console.log('请求失败') + }) + } + ) +``` \ No newline at end of file -- Gitee From 9e17be398d82412ae4f19aa597df2d5720ea1168 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E7=9D=BF?= <2476310189@qq.com> Date: Fri, 2 Dec 2022 14:29:19 +0000 Subject: [PATCH 4/5] =?UTF-8?q?=E6=96=B0=E5=BB=BA=202022-12-01=20Promise?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../\347\254\224\350\256\260/2022-12-01 Promise/.keep" | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 "06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-12-01 Promise/.keep" diff --git "a/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-12-01 Promise/.keep" "b/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-12-01 Promise/.keep" new file mode 100644 index 0000000..e69de29 -- Gitee From b7f6d96fa15393600ce080b32824017559b59ff6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=95=E7=9D=BF?= <2476310189@qq.com> Date: Fri, 2 Dec 2022 14:29:34 +0000 Subject: [PATCH 5/5] =?UTF-8?q?=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 何睿 <2476310189@qq.com> --- .../2022-12-01 \347\254\224\350\256\260.md" | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 "06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-12-01 Promise/2022-12-01 \347\254\224\350\256\260.md" diff --git "a/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-12-01 Promise/2022-12-01 \347\254\224\350\256\260.md" "b/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-12-01 Promise/2022-12-01 \347\254\224\350\256\260.md" new file mode 100644 index 0000000..35b6a03 --- /dev/null +++ "b/06\344\275\225\347\235\277/\347\254\224\350\256\260/2022-12-01 Promise/2022-12-01 \347\254\224\350\256\260.md" @@ -0,0 +1,48 @@ +## Promise + +### PromiseState + +在每个**promise**对象中,状态只会改变一次 + +**pending**:准备中 + +**fulfilled \**(成功) :调用了\**resolve**函数,将状态改为成功 + +**rejected** (失败) :调用了**reject**函数,将状态改为失败 + +### PromiseResult + +接收**resolve**或者**reject**中的参数 + +```js +const p = new Promise((resolve, reject) => { + // resolve('这是成功'); + reject('这是失败'); +}).then(data =>{ + console.log('成功then调用'); + return 123 + //catch:捕获异常 +}).catch( + function (e) { + console.log(e); + } +) +//then(成功时调用的函数,失败时调用的函数),返回值也是一个promise对象,pending +console.log(p) +``` + +## GET + +```js +$('div').eq(0).one('mousemove', function () { + $.get('data.json', function (data) { + const { username } = data; + $.get('data1.json', function (data1) { + const contact = data1[username] + $.get('data2.json', function (data2) { + console.log(username + ':' + data2[contact]); + }) + }) + }) +}) +``` \ No newline at end of file -- Gitee