1、GET请求传参数给后端
2、参数会拼接到url中,不安全,速度快
3、后端返回的数据 前端是xhr对象接受了,程序员用js语言来使用返回的数据
3.浏览器的地址栏[只能]发送get请求,
接受的数据会直接读取渲染,如果解析失败会下载
- var searvalue=document.getElementById("seariput").value //从页面中获取的参数值
- var xhr=new XMLHttpRequest()
- var url=`网址?keywords=${searvalue}`
- xhr.open("GET",url)
- xhr.send()
- xhr.onreadystatechange=()=>{
- if(xhr.readyState==4&&xhr.status==200){
- console.log(xhr.responseText)
- }
- }
用法:axios(url,{params:{ } }),也可以为axios.get(url,{params:{ } })
( 它是ajax技术封装出来的(更好用))
- // 后端返回的数据 是前端xhr对象接收了,程序员用js语言来使用返回的数据
- var searvalue=document.getElementById("seariput").value
- var url=`http://192.168.6.60:7001/get1`
- axios(url,{params:{count:20,keyw:searvalue}})
- .then(res=>console.log(res))
1、a标签的href属性也[只能]发get请求,并且是点击事件触发了默认事件才会发送get请求
2、发送网络请求给href的网址,后端返回的数据,接受的数据会直接读取渲染,如果解析失败会下载
[只能]发get请求 返回的数据渲染成图片 如果非图片编码就会"碎裂"
img.οnlοad=()=>{}
[只能]发get请求 返回的数据按照功能使用
发送GET/POST/DELETE等等给action属性对应的url 发送请求
1.用户点击了提交按钮或者触发表单的提交事件
2.get请求会把form中的数据全部解析为url的querystring
3.返回的数据 同a标签
this.ctx.request.query
如果后端的controlner中写接受字段方法如图:
- async get1() {
- console.log(666)
- //1.egg后端get请求的参数:
- var k = this.ctx.request.query //k则为接受的字段
- console.log(k, 1111) //如果服务器收到,则打印出来
- this.ctx.body = {
- info: "get1接口的数据" //返回给前端的数据
- }
- }
如果前端的表单中绑定一个点击事件为axios的函数
- function fn() {
- console.log(666)
- var searvalue=document.getElementById("seariput").value //为获取表单的值
- var url=`http://192.168.31.224:7001/get1`
- axios(url,{params:{count:20,keyw:searvalue}})
- .then(res=>console.log(res.data))
-
- }
前端页面写入的内容为0730:
点击搜索,那么服务器就会收到并打印:
前端页面控制台会打印: