jQuery的下载
以前直接点击就可以下载jquery.js,现在点击后无法下载了,只能进行jquery文件的查看,这时候就需要自己创建一个.js后缀的文本,点击下载链接复制内容到创建的文本中,然后保存进行使用。
ajax的使用
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<script src="./lib/jquery.js"></script>
</head>
<body>
<button id="btn_get">发起不带参数的GET请求</button>
<script>
$(function () {
$('#btn_get').on('click', function () {
$.get('http://www.liulongbin.top:3006/api/getbooks', function (res) {
console.log(res)
//{"status":200,"msg":"获取图书列表成功","data":[{"id":1,"bookname":"西游记","author":"吴承恩",
// "publisher":"北京图书出版社"},{"id":2,"bookname":"红楼梦","author":"曹雪芹","publisher":"上海图书出版社"},
//{"id":3,"bookname":"三国演义","author":"罗贯中","publisher":"北京图书出版社"},{"id":111,"bookname":"背影",
//"author":"朱自清","publisher":"中国文化出版社"}]}
})
})
})
</script>
</body>
</html>
使用GET发起不带参数的请求
<body>
<button id="btn_get">发起不带参数的GET请求</button>
<script>
$(function () {
$('#btn_get').on('click', function () {
$.get('http://www.liulongbin.top:3006/api/getbooks', function (res) {
console.log(res)
//{"status":200,"msg":"获取图书列表成功","data":[{"id":1,"bookname":"西游记","author":"吴承恩",
// "publisher":"北京图书出版社"},{"id":2,"bookname":"红楼梦","author":"曹雪芹","publisher":"上海图书出版社"},
//{"id":3,"bookname":"三国演义","author":"罗贯中","publisher":"北京图书出版社"},{"id":111,"bookname":"背影",
//"author":"朱自清","publisher":"中国文化出版社"}]}
})
})
})
</script>
</body>
使用GET发起带参数的请求
<body>
<button id="btn_get">发起带参数的GET请求</button>
<p id="p_result">这是请求的结果</p>
<script>
$(function () {
$('#btn_get').on('click', function () {
$.get('http://www.liulongbin.top:3006/api/getbooks', { id: 1 }, function (res) {
console.log(res)
var getbooksData = JSON.parse(JSON.stringify(res));
var data = getbooksData.data;
var log = "作者:" + data[0].author + "; 作品:" + data[0].bookname + "; 出版社:" + data[0].publisher;
document.getElementById('p_result').innerHTML = log;
console.log(log)//{"status":200,"msg":"获取图书列表成功","data":[{"id":1,"bookname":"西游记","author":"吴承恩","publisher":"北京图书出版社"}]}
})
})
})
</script>
</body>
使用POST提交数据
<body>
<button id="btn_post">发起POST请求</button>
<script>
$(function () {
$('#btn_post').on('click', function () {
console.log('点击了')
$.post('http://www.liulongbin.top:3006/api/addbook', { bookname: '背影', author: '朱自清', publisher: '中国文化出版社' }, function (res) {
var data = JSON.parse(JSON.stringify(res))
console.log(res);//{"status":201,"msg":"添加图书成功"}
alert(data.msg)
})
})
})
</script>
</body>
使用$.ajax发起GET请求
<body>
<button id="btn_get">发起GET请求</button>
<script>
$(function () {
$('#btn_get').on('click', function () {
$.ajax({
type: 'GET',
url: 'http://www.liulongbin.top:3006/api/getbooks',
data: {
id: 1
},
success: function (res) {
console.log(res)//{"status":200,"msg":"获取图书列表成功","data":[{"id":1,"bookname":"西游记","author":"吴承恩","publisher":"北京图书出版社"}]}
}
})
})
})
</script>
</body>
使用$.ajax发起POST请求
<body>
<button id="btn_post">发起GET请求</button>
<script>
$(function () {
$('#btn_post').on('click', function () {
$.ajax({
type: 'POST',
url: 'http://www.liulongbin.top:3006/api/addbook',
data: {
bookname: '前面就是麦季',
author:'李骏虎',
publisher:'黄河出版社'
},
success: function (res) {
console.log(res)//{"status":201,"msg":"添加图书成功"}
}
})
})
})
</script>
</body>