【AJAX】使用AJAX发送GET/DELETE/POST/PUT请求的方法和步骤

一.用AJAX发送GET请求

  • 创建对象  var xhr = new XMLHttpRequest()
  • 建立连接  xhr.open(GET,url,true)  true:使用异步方式发送数据  false:使用同步方式发送数据 可以省略不写,默认异步方式
  • 准备预处理方案  xhr.onload = function(){ console.log(xhr.responseText) }
  • 发送请求消息

二.用AJAX发送DELETE请求

与GET 方法一致,只需要修改方法名为DELETE

三.用AJAX发送POST请求

  • 创建对象  var xhr = new XMLHttpRequest()
  • 建立连接  xhr.open(POST,url)
  • 修改请求头信息   xhr.setRequestHeader('Content-type','application/x-www-form-urlencoded;charset=UTF-8')
  • 准备预处理方案  xhr.onload = function(){ console.log(xhr.responseText) }
  • 发送请求消息  xhr.send(data)     data格式:   key1=value2&key2=value2  

四.用AJAX发送PUT请求

与POST 方法一致,只需要修改方法名为PUT

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