From bbf3f460c59df4b99d6ad404ed70c5bc278acd3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=A8=8B=E5=B8=85=E7=BF=94?= <16699465019@163.com> Date: Wed, 13 Dec 2023 12:14:23 +0800 Subject: [PATCH] 12.12 --- .../12.12.md" | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 "52\345\217\267 \347\250\213\345\270\205\347\277\224/12.12.md" diff --git "a/52\345\217\267 \347\250\213\345\270\205\347\277\224/12.12.md" "b/52\345\217\267 \347\250\213\345\270\205\347\277\224/12.12.md" new file mode 100644 index 0000000..270b9a4 --- /dev/null +++ "b/52\345\217\267 \347\250\213\345\270\205\347\277\224/12.12.md" @@ -0,0 +1,32 @@ +# 作业 + +```js + +// 1. 定义myAxios函数,接收配置对象,返回Promise对象 +function myAxios(config) { + return new Promise((resolve, reject) => { + // 2. 发起XHR请求,默认请求方法为GET + const xhr = new XMLHttpRequest() + xhr.open(config.method || 'GET', config.url) + xhr.addEventListener('loadend', () => { + // 3. 调用成功/失败的处理程序 + if (xhr.status >= 200 && xhr.status < 300) { + resolve(JSON.parse(xhr.response)) + } else { + reject(new Error(xhr.response)) + } + }) + xhr.send() + }) +} + +// 4. 使用myAxios函数,获取省份列表展示 +myAxios({ + url: 'http://hmajax.itheima.net/api/province' +}).then(result => { + console.log(result) + document.querySelector('.my-p').innerHTML = result.list.join('
') +}).catch(error => { + console.log(error) + document.querySelector('.my-p').innerHTML = error.message +}) \ No newline at end of file -- Gitee