From cc7692421baa2a6ab7f3b041bdcd492a90bcdb81 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=81=E7=AB=A0=E5=BD=AC?= <2629351295@qq.com> Date: Fri, 2 Dec 2022 12:54:52 +0000 Subject: [PATCH 1/2] =?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: 翁章彬 <2629351295@qq.com> --- .../2022-11-30 \347\254\224\350\256\260.md" | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 "01\347\277\201\347\253\240\345\275\254/\347\254\224\350\256\260/2022-11-30 \347\254\224\350\256\260.md" diff --git "a/01\347\277\201\347\253\240\345\275\254/\347\254\224\350\256\260/2022-11-30 \347\254\224\350\256\260.md" "b/01\347\277\201\347\253\240\345\275\254/\347\254\224\350\256\260/2022-11-30 \347\254\224\350\256\260.md" new file mode 100644 index 0000000..0eb3504 --- /dev/null +++ "b/01\347\277\201\347\253\240\345\275\254/\347\254\224\350\256\260/2022-11-30 \347\254\224\350\256\260.md" @@ -0,0 +1,130 @@ +### 一,原生AJAX + +#### 1,GET方法;传送数据量小,处理效率高,安全性低,会被缓存 + +- send()中不需要添加任何参数,因为在连接服务器的时候就已经发送 +- get请求就不必要设置 xhr.setRequestHeader(header,value) + +``` +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) + +``` + 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,手动转换 + +``` +var obj = JSON.parse(xhr.response) +console.log(obj.name) +``` + +##### 2,自动转换 + +``` +//创建对象后设置: +xhr.responseType = 'json'; +console.log(xhr.response.name) +``` + +### 二,jQuery Ajax + +#### 1,$.get();执行get请求方式的ajax + +``` + $('button').eq(0).click( + function () { + $.get('data.json',{a:100,b:200},function (data) { + console.log(data); + },'json'); + } + ) +``` + +#### 2,$.post();执行post请求方式的ajax + +``` + $('button').eq(1).click( + function () { + $.post('data.json',{a:100,b:200},function (data) { + console.log(data); + },'json'); + } + ) +``` + +#### 3,$.getJSON() + +- 通过Ajax获取服务器中JSON格式的数据 +- “全局方法”,直接定义在jQuery对象(即“$”)下的方法 + +``` + $('button').eq(2).click( + function () { + $.getJSON('data.json',function (data) { + console.log(data); + }) + } + ) +``` + +#### 4,$.getScript();动态加载JavaScript的方法 + +- 通过AJAX来获取并运行一个外部JavaScript文件 +- 异步跨域加载JavaScript文件,可避免提前加载JavaScript文件 +- 减少服务器和客户端的负 担,加快页面加载速度 + +``` + $('button').eq(3).click( + function () { + $.getScript('test.js'); + } + ) +``` + +#### 5,$.ajax();核心方法,所有的其他方法都是在内部使用此方法 + +- 以上方法用ajax()都能实现 + +``` + $('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 65e5758159ce6adbc706e95227e5269bb6eccded Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=BF=81=E7=AB=A0=E5=BD=AC?= <2629351295@qq.com> Date: Fri, 2 Dec 2022 13:06:03 +0000 Subject: [PATCH 2/2] =?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: 翁章彬 <2629351295@qq.com> --- ...12-01 Promise \347\254\224\350\256\260.md" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "01\347\277\201\347\253\240\345\275\254/\347\254\224\350\256\260/2022-12-01 Promise \347\254\224\350\256\260.md" diff --git "a/01\347\277\201\347\253\240\345\275\254/\347\254\224\350\256\260/2022-12-01 Promise \347\254\224\350\256\260.md" "b/01\347\277\201\347\253\240\345\275\254/\347\254\224\350\256\260/2022-12-01 Promise \347\254\224\350\256\260.md" new file mode 100644 index 0000000..af72e01 --- /dev/null +++ "b/01\347\277\201\347\253\240\345\275\254/\347\254\224\350\256\260/2022-12-01 Promise \347\254\224\350\256\260.md" @@ -0,0 +1,49 @@ +## Promise + +### PromiseState + +在每个**promise**对象中,状态只会改变一次 + +**pending**:准备中 + +**fulfilled **(成功) :调用了**resolve**函数,将状态改为成功 + +**rejected** (失败) :调用了**reject**函数,将状态改为失败 + +### PromiseResult + +接收**resolve**或者**reject**中的参数 + +```html +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 + +``` +$('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]); + }) + }) + }) +}) +``` + -- Gitee