了解服务器:提供某种服务器的机器(计算机)
1.访问服务器的几种方式
- 直接在地址栏输入网址
- a标签的href属性
- Location.href = ‘ url ‘
- Ajax 网页不会跳转
Ajax技术:在网页不跳转的情况下,向服务器请求数据
应用场景:局部刷新
2.Ajax语法:
get
使用内置对象XMLHttpRequest发送ajax请求
1.创建XMLHttpRequest对象
Let xhr = new XMLHttpRequest( )
2.设置请求方法和地址
Xhr.open( ‘get’ , ‘网页地址’)
- 发送请求
Xhr.send( )
- 注册响应事件
Xhr.οnlοad= function( ) {
Console.log( Xhr.responseText)
进行josn转化成js
Let res = JSON.prase( Xhr.responseText )
输出看下信息
Console.log( res )
}
Post
1.创建XMLHttpRequest对象
Let xhr = new XMLHttpRequest( )
2.设置请求方法和地址
Xhr.open( ‘post’ , ‘网页地址’)
3设置请求头
xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded')
4.发送请求
Xhr.send( )
- 注册响应事件
Xhr.οnlοad= function( ) {
Console.log ( Xhr.responseText)
进行josn转化成js
Let res = JSON.prase( Xhr.responseText )
输出看下信息
Console.log( res )
}
3.一个标准的接口文档至少要包含以下三种信息
a.请求的地址 (url)
b.请求的方法 (get或者post)
c.请求的参数
4.前后端交互的三个流程
- 请求(浏览器)
- 处理(服务器)
- 响应(服务器)