在使用ajax方法进行数据交互的时候,会用到open方法,其中的第一个参数有 post和get两种传参方式,有一定的区别
get请去的传参数 在网址中拼接参数 第一个参数用? 后面的用& 每一对参数用=连接
get请求传输的数据不安全 但是它传输比较快
post请去的传参数 在send函数中传输 会把数据打包到请求体中,会加密变成暗文 就比较安全
post 传数据的方式比get慢
function get1 () {
var xhr=new XMLHttpRequest()
xhr.open("GET","http://192.168.5.109:8081/ajax1?type=3&count=20",true)
xhr.send()
xhr.onreadystatechange=function () {
if(xhr.readyState==4){
console.log(xhr.responseText)
}
}
}
function post1 () {
var xhr=new XMLHttpRequest()
xhr.open("POST","http://192.168.5.109:8081/ajax2?type=3&count=20",true)
xhr.send("pwd=abc123&userid=12345")
xhr.onreadystatechange=function () {
if(xhr.readyState==4){
console.log(xhr.responseText)
}
}
}
在route.js中,方法这样用
ajax2(req,res){
if(req.method=="POST"){
console.log(req.url)
var msg="";
req.on("data",(data)=>{
msg+=data
})
req.on("end",()=>{
console.log(msg)
})
}
res.end('{"msg":"ajax2的数据"}')
}