124.AJAX_请求错误的处理
2023-09-27 14:23:04 时间
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>请求错误处理</title>
</head>
<body>
<h1>AJAX请求失败处理</h1>
<input type="button" value="获取响应数据" onclick="get_data()">
<script>
function get_data() {
// 创建xhr对象
let xhr = new XMLHttpRequest()
// 设置请求方式与请求地址
// 测试1:正常请求
xhr.open('get','http://httpbin.org/get')
// 测试2:404 请求地址错误
xhr.open('get','http://httpbin.org/getfjaksfhkas')
// 测试3:网络中断 无法发送到服务端
xhr.open('get','http://httpbin.orgfasfaa/get')
//发送请求
xhr.send()
// 获取服务器响应
xhr.onreadystatechange = () =>{
//判断AJAX状态码
if (xhr.readyState == 4){
console.log(xhr.status);
}
}
// 请求失败的监听函数
xhr.onerror=()=>{
console.log("网络中断");
}
}
</script>
</body>
</html>
相关文章
- Nacos客户端升级到2.x版本后,启动出现9848端口错误解决
- 我最在行 诗词 连续错误的
- distribution was not found and is required by the application错误
- Visual Studio 2015 Update 1 安装到最后 KB3022398 错误解决方法
- .Net Core 控制台程序错误:Can not find runtime target for framework '.NETCoreApp,Version=v1.0' compatible with one of the target runtimes: 'win10-x64, win81-x64, win8-x64, win7-x64'.
- eclipse导入maven项目报错"Missing artifact com.sun.:tools:jar:1.8.0"错误解决方法
- QT中关于类静态成员的编码格式错误(静态成员初始化必须放在.cpp文件中)
- [错误]Caused by: org.apache.spark.memory.SparkOutOfMemoryError: Unable to acquire 65536 bytes of memory, got 0
- git拉取项目用户名或密码输入错误
- 《C语言解惑》—— 2.3 输入的格式配对错误
- 【hacker的错误集】html5lib使用报错Couldn‘t find a tree builder with the features you requested: html5lib
- Linux 环境下 javac 编译错误: 编码UTF8的不可映射字符 (编码UTF8/GBK的不可映射字符)
- ajax:post 400错误
- 【AD错误】Un-Routed Net Constraint: Unplated Pad S1-1(2.54mm,20.322mm) on Multi-Layer...解决办法
- 代理错误[WinError 10061]
- ESP32 esp-modem: Ring Buffer Full 错误的修复