- 使用 index.js 暴露的 getJSON() 进行测试。
- 测试成功状态。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>测试封装好的 Ajax</title>
</head>
<body>
<script type="module">
import { ajax, get, getJSON, post } from './ajax/index.js';
const url = 'https://www.imooc.com/api/http/search/suggest?words=js';
const p = getJSON(url, {
params: { username: 'alex' },
data: { age: 18 }
});
const { ERROR_HTTP_CODE, ERROR_REQUEST, ERROR_TIMEOUT, ERROR_ABORT } = p;
p.then(repsonse => {
console.log(repsonse);
}).catch(err => {
switch (err.type) {
case ERROR_HTTP_CODE:
console.log(err.text);
break;
case ERROR_REQUEST:
console.log(err.text);
break;
case ERROR_TIMEOUT:
console.log(err.text);
break;
case ERROR_ABORT:
console.log(err.text);
break;
}
});
</script>
</body>
</html>
![](https://img-blog.csdnimg.cn/20211004155003761.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5bCP6LGqYm95,size_20,color_FFFFFF,t_70,g_se,x_16)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>测试封装好的 Ajax</title>
</head>
<body>
<script type="module">
import { ajax, get, getJSON, post } from './ajax/index.js';
// 当前目录下没有 hello.html
const url = './hello.html';
const p = getJSON(url, {
params: { username: 'alex' },
data: { age: 18 }
});
const { ERROR_HTTP_CODE, ERROR_REQUEST, ERROR_TIMEOUT, ERROR_ABORT } = p;
p.then(repsonse => {
console.log(repsonse);
}).catch(err => {
switch (err.type) {
case ERROR_HTTP_CODE:
console.log(err.text);
break;
case ERROR_REQUEST:
console.log(err.text);
break;
case ERROR_TIMEOUT:
console.log(err.text);
break;
case ERROR_ABORT:
console.log(err.text);
break;
}
});
</script>
</body>
</html>
![](https://img-blog.csdnimg.cn/20211004155614741.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5bCP6LGqYm95,size_20,color_FFFFFF,t_70,g_se,x_16)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>测试封装好的 Ajax</title>
</head>
<body>
<script type="module">
import { ajax, get, getJSON, post } from './ajax/index.js';
// 输入错误的请求地址
const url = 'https://www.iiiimooc.com/api/http/search/suggest?words=js';
const p = getJSON(url, {
params: { username: 'alex' },
data: { age: 18 }
});
const { ERROR_HTTP_CODE, ERROR_REQUEST, ERROR_TIMEOUT, ERROR_ABORT } = p;
p.then(repsonse => {
console.log(repsonse);
}).catch(err => {
switch (err.type) {
case ERROR_HTTP_CODE:
console.log(err.text);
break;
case ERROR_REQUEST:
console.log(err.text);
break;
case ERROR_TIMEOUT:
console.log(err.text);
break;
case ERROR_ABORT:
console.log(err.text);
break;
}
});
</script>
</body>
</html>
![](https://img-blog.csdnimg.cn/2021100415581516.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5bCP6LGqYm95,size_20,color_FFFFFF,t_70,g_se,x_16)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>测试封装好的 Ajax</title>
</head>
<body>
<script type="module">
import { ajax, get, getJSON, post } from './ajax/index.js';
const url = 'https://www.imooc.com/api/http/search/suggest?words=js';
const p = getJSON(url, {
params: { username: 'alex' },
data: { age: 18 },
timeoutTime: 10
});
const { ERROR_HTTP_CODE, ERROR_REQUEST, ERROR_TIMEOUT, ERROR_ABORT } = p;
p.then(repsonse => {
console.log(repsonse);
}).catch(err => {
switch (err.type) {
case ERROR_HTTP_CODE:
console.log(err.text);
break;
case ERROR_REQUEST:
console.log(err.text);
break;
case ERROR_TIMEOUT:
console.log(err.text);
break;
case ERROR_ABORT:
console.log(err.text);
break;
}
});
</script>
</body>
</html>
![](https://img-blog.csdnimg.cn/20211004161334793.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5bCP6LGqYm95,size_20,color_FFFFFF,t_70,g_se,x_16)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>测试封装好的 Ajax</title>
</head>
<body>
<script type="module">
import { ajax, get, getJSON, post } from './ajax/index.js';
const url = 'https://www.imooc.com/api/http/search/suggest?words=js';
const p = getJSON(url, {
params: { username: 'alex' },
data: { age: 18 },
});
p.xhr.abort();
const { ERROR_HTTP_CODE, ERROR_REQUEST, ERROR_TIMEOUT, ERROR_ABORT } = p;
p.then(repsonse => {
console.log(repsonse);
}).catch(err => {
switch (err.type) {
case ERROR_HTTP_CODE:
console.log(err.text);
break;
case ERROR_REQUEST:
console.log(err.text);
break;
case ERROR_TIMEOUT:
console.log(err.text);
break;
case ERROR_ABORT:
console.log(err.text);
break;
}
});
</script>
</body>
</html>
![](https://img-blog.csdnimg.cn/20211004161437257.png?x-oss-process=image/watermark,type_ZHJvaWRzYW5zZmFsbGJhY2s,shadow_50,text_Q1NETiBA5bCP6LGqYm95,size_20,color_FFFFFF,t_70,g_se,x_16)