<!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> Cors</title>
</head>
<body>
<script>
// https://jsonplaceholder.typicode.com/todos/1
// 核心四步
// 01 创建变量
let xhr =new XMLHttpRequest;
// 02 建立连接
xhr.open('get','https://jsonplaceholder.typicode.com/todos/1');
// 03 监听
xhr.onreadystatechange=function(){
if(/^2\d{2}$/.test(xhr.status) && xhr.readyState==4){
console.log(xhr.responseText)
}
};
// 04发送请求
xhr.send();
</script>
</body>
</html>
<!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>Document</title>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
</head>
<body>
<script>
$.ajax({
type: "GET",
url: 'https://jsonplaceholder.typicode.com/todos/1',
//url: 'http://data.cma.cn/data/cdcdetail/dataCode/NOCV_SURF_CLI_GLB_MUL_DAY.html',
//url: 'https://www.baidu.com/',
success: function (response, status, xhr) {
//服务器返回的信息
console.log(response);
//状态码
console.log(status);
//状态码
console.log(xhr.status);
//响应头部
console.log(xhr.getAllResponseHeaders());
},
error: function () {
console.log("请求失败");
}
});
</script>
</body>
</html>
https://www.runoob.com/jquery/ajax-ajax.html
参考:
5星
周啸天 珠峰
