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 0000000000000000000000000000000000000000..270b9a4dffce69f74ef6ec9bde66767e7e5810f4 --- /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