查询字符串
1.什么是查询字符串
定义:查询字符串(URL参数)是指在URL的末尾加上用于向服务器发送信息的字符串(变量)。
格式:将英文的?放在URL的末尾,然后再加上参数=值,想加上多个参数的话,使用&符号进行分隔。以这个形式,可以将想要发送给服务器的数据添加到URL中。
2.GET请求携带参数的本质
无论使用$.ajax(),还是使用$.get(),又或者直接使用xhr对象发起GET请求,当需要携带参数的时候,本质上,都是直接将参数以查询字符串的形式,追加到URL地址的后面,发送到服务器的。
代码示例:
<!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="/js文件/jquery-3.6.0.js"></script> </head> <body> </body> <script> // $.get('http://www.liulongbin.top:3006/api/getbooks', { // id: 1, // bookname: '西游记' // }, function (res) { // console.log(res); // }) $.ajax({ method: 'GET', url: 'http://www.liulongbin.top:3006/api/getbooks', data: { id: 1, bookname: '西游记' }, success: function (res) { console.log(res); } }) </script> </html>