<!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>原生ajax</title>
<script src="./js/qs-6.9.4.js"></script>
</head>
<body>
<script>
let qs = Qs;
// 创建ajax实例对象
let xhr = new XMLHttpRequest();
//打开一个连接 请求方式 请求路径
// get传递参数 查询字符串 使用qs进行转换
let params={
page:1,
pageSize:10
};
let qsObj=qs.stringify(params);
console.log(qsObj,'查询字符串');
xhr.open('get','http://121.199.0.35:8888/index/carousel/findAll'+'?'+qsObj);
//xhr.open('get','http://121.199.0.35:8888/index/carousel/findAll');
// 设置请求头 --表单格式数据
// xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
// xhr.setRequestHeader('Content-Type','application/json');
// 发送请求 post携带参数携带请求体中
// xhr.send(qs.stringify({id:1,name:'zhangsan'}));
xhr.send();
// xhr.send(JSON.stringify({id:1,name:'zhangsan'}));
// 接收响应
xhr.onreadystatechange = function(){
if(xhr.readyState === 4 && xhr.status === 200){
console.log(xhr.responseText);
}
}
</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="../day07/js/qs-6.9.4.js">
</script>
</head>
<body>
<!-- 复习ajax -->
<script>
let qs=Qs;
let params={
page:1,
pageSize:10
};
let qsobject=qs.stringify(params);
console.log(qsobject,'查询字符串');
//1.创建ajax对象
let xhr=new XMLHttpRequest();
//2.打开一个链接 请求方式 请求路径
//get 传递参数 查询字符串 使用qs进行转换
// xhr.open('get','http://121.199.0.35:8888/index/carousel/findAll'+"?"+qsobject)
xhr.open('post','http://121.199.0.35:8888/index/carousel/findAll')
//设置请求头
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded') //表单格式数据
xhr.setRequestHeader('Content-Type','application/json')
//3.发送请求
// xhr.send(); //get 方式发送请求
xhr.send(qs.stringify({id:1,name:'gyy'})) //post发送请求 -----表单格式
xhr.send(JSON.stringify({id:1,name:'gyy'})) //post发送请求 -----Json格式
xhr.onreadystatechange=function(){
if(xhr.readyState=== 4&&xhr.status ===200){
console.log(xhr.responseText);
}
}
</script>
</body>
</html>