查看接口文档
发送post请求 查看文档
<script>
/*
* GET 和 post 请求
*/
//1. GET请求
//var xhr = new XMLHttpRequest()
//需要携带参数
//因为是GET请求,直接在地址后面进行参数的书写
//xhr.open('GET','http://localhost:8888/test/third?name=游坦之&age=18',true)
//xhr.onload = function(){
//console.log(JSON.parse(xhr.responseText))
//}
//xhr.send()
//2. POST
var xhr = new XMLHttpRequest()
//注意:因为是POST请求,不必须要在地址后面拼接参数
xhr.open('POST','http://localhost:8888/test/fourth',true)
xhr.onload=function(){
cosole.log(JSON.parse(xhr.responseText))
}
// 注意: 当你发送POST请求,并且需要携带参数的时候,需要进行特殊说明
//语法: xhr.setRequestHeader('content-type',你传递参数的格式)
xhr.setRequestHeader('content-type','application/x-www-form-urlencoded')
//send 后面的()就是书写请求体的位置
xhr.send('name=游坦之&age=18')
</script>