<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>请求方式</title>
</head>
<body>
</body>
<script>
console.log('---------get请求和ppost请求----------------');
// get请求
//创建
var xhr = new XMLHttpRequest();
//配置请求信息
//需要携带参数
//直接在地址后面添加 ? 直接书写 参数=属性& 参数1=属性1
xhr.open('get', '地址?name=大白&age=15', true);
//注册完成事件
xhr.onload = function() {
console.log(JSON.parse(xhr.responseText));
}
//发送请求
xhr.send();
// post请求
//创建
var aj = new XMLHttpRequest();
//配置请求信息
//需要携带参数
//因为是post请求 不能直接在地址后面添加
xhr.open('get', '地址', true);
//注册完成事件
xhr.onload = function() {
console.log(JSON.parse(xhr.responseText));
}
//当你发送 post请求 并且需要传递参数时, 徐奥特殊说明
// 语法 : aj.setRequestHeader('content-type',传递参数的格式后端有);
aj.setRequestHeader('content-type', 传递参数的格式后端有);
//发送请求 并在send的后面的() 书写请求体的位置
xhr.send('name=大笔&age=15');
</script>
</html>
GET和POST请求方式
最新推荐文章于 2022-12-30 11:10:17 发布