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" 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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 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 0000000000000000000000000000000000000000..87d3f33e73a4c85a9bdaceb367dc28f8863842be --- /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 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 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 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 0000000000000000000000000000000000000000..35b6a037dd1e10d129eb4dfa02639832b08e0f5b --- /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