Ajax基础语法总结

了解服务器:提供某种服务器的机器(计算机)

1.访问服务器的几种方式

  1. 直接在地址栏输入网址
  2. a标签的href属性
  3. Location.href = ‘ url ‘
  4. Ajax  网页不会跳转

Ajax技术:在网页不跳转的情况下,向服务器请求数据

应用场景:局部刷新

2.Ajax语法:

get

使用内置对象XMLHttpRequest发送ajax请求

1.创建XMLHttpRequest对象

Let xhr = new XMLHttpRequest( )

2.设置请求方法和地址

Xhr.open( ‘get’ , ‘网页地址’)

  1. 发送请求

Xhr.send( )

  1. 注册响应事件

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( )

  1. 注册响应事件

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.前后端交互的三个流程

  1. 请求(浏览器)
  2. 处理(服务器)
  3. 响应(服务器)

你可能感兴趣的:(ajax,前端)