当前位置: 首页 > article >正文

【前端】前后端通信

前端开发主要完成的两件事:
1)界面搭建
2)数据交互
本知识页参考:
https://juejin.cn/post/6925296067378429960

0. XMLHttpRequest

  1. 客户端的一个API,为浏览器和服务器通信提供了一个便携通道。现代浏览器支持XMLHttpRequest API,如IE 7+、
    1. 创建XMLHttpRequest对象:在后台与服务器交换数据,创建XMLHttpRequest对象的方法如下
    var xhr = new XMLHttpRequest()
    
    1. 建立连接
    function createXHR() {var XHR = [function() { return new XMLHttpRequest() },function() { return new ActiveXObject ("Msxml2.XMLHTTP") },function() {return new ActiveXObject ("Msxml3.XMLHTTP")},function () { return new ActiveXObject ("Microsoft.XMLHTTP") }]var xhr = null// 尝试调用函数for (var i = 0; i <XHR.length; i++) {try {xhr = XHR[i]()} catch (e) {continue}break}return xhr
    }
    // 建立连接
    xhr.open(method, url, async, username, password)
    // 其中xhr是XMLHttp
    xhr.send(body) // 
    // 异步通信
    let xhr = createXHR()
    xhr.open("GET", "server.txt", "false")
    xhr.send(null)
    console.log(xhr.responseText) 
    
    1. 发送请求
      1. get请求
        1. 简单字符串,不适用大容量或加密数据
      2. post请求:发送任意类型、长度的数据,多用于表单提交,以send()方法传递而不是查询字符串
      
      
      1. 获取HTML字符串
       <table border="1" width="100%"><tr><td>RegExp.exec()</td>通用的匹配模式</tr><tr><td>RegExp.exec()</td></tr></table>
      
      1. 获取和设置头部消息
      var xhr = createXHR()
      var url = "server.txt"
      xhr.open()
      xhr.onreadystatechange = function() {if(xhr.readyState==4&&xhr.status==200) {console.log(xhr.getAllResponseHeaders())}
      }
      xhr.send(null)
      
      1. getResponseHeader()
      [{user:"css8", pass}]
      
      1. Header-name表示头部消息名称
        1. value表示消息的具体值
          let xhr = createXHR()
          let url = "server.txt"
          
        2. 使用post方法
          
          
        3. fetch()
        4. XMLHttpRequest
    2. 串行格式化
      1.

1. Ajax

使用AJAX(Asynchronous Javascript And XML),使用XMLHttpRequest 技术构建更复杂,动态的网页的编程实践。
XHR由微软引入,首次在请求时,用于与服务器交互。

  1. 特点:
    1. XHR:在不刷新页面的情况下,请求URL获取服务器数据,
  2. 除JS外,jQuery、Axios、vue-resource 都可以实现 Ajax 编程
  3. 共性

1.1 原生JS

  1. 使用方式
var url = 'https://api.github.com/users/chriscoyier/repos'
var xhr = new XMLHttpRequest()
xhr.open('GET', url, false)
xhr.onreadystatechange = function() {if(xhr.readyState === 4) {if(xhr.status === 200 || xhr.status === 304) {console.log('response:', xhr.response)}}
}
xhr.send()
  1. JS封装
var Ajax = {get: function(url, callback) {var xhr = new XMLHttpRequest()xhr.open('GET', url, false)xhr.onreadystatechange = function() {if(xhr.readyState === 4) {if(xhr.status === 200 || xhr.status === 304) {console.log(xhr.response)callback(xhr.response)}}}xhr.send()},post: function(url, data, callback) {var xhr = new XMLHttpRequest()xhr.open('POST', url, false)xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded')xhr.onreadystatechange = function() {if (xhr.readyState === 4) {if (xhr.status === 200 || xhr.status === 304) {console.log(xhr.response)callback(xhr.response)}}}xhr.send(data)}
}

1.2 JQuery

  1. 实现Ajax的方法
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.5.1/jquery.js"></script>
<script>$(function(){const list = {}const url = ''$.ajax({type: "POST",contentType: "application/json;charset=UTF-8",url: url,data: JSON.stringify(list),success: res => console.log('res', res),error: err => console.log('err', err)})})
</script>
  1. vue-resource

1.3 Axios

  1. axios基本特性
    1.
  2. 基本用法
  3. 常用API
  4. axios参数传递
  5. axios响应结果
  6. axios全局配置
  7. axios拦截器

1.4 Promise

  1. 使用ES6的Promise完成GET
  2. 1
  3. 1

2. Fetch


3.

4. 写的好累啊!

5. 实时通信的实现

本部分知识点参考:https://blog.csdn.net/dyk11111/article/details/134007708

5.1 WebSocket

5.2 轮询(poll)

5.3 长轮询

http://www.lryc.cn/news/2400669.html

相关文章:

  • 编程技能:格式化打印04,sprintf
  • C语言基础(11)【函数1】
  • R语言基础| 下载、安装
  • 【hive sql】窗口函数
  • Ubuntu24.04 交叉编译 aarch64 ffmpeg
  • 《AI角色扮演反诈技术解析:原理、架构与核心挑战》
  • 微软的新系统Windows12未来有哪些新特性
  • 树莓派超全系列教程文档--(54)如何使用rsync在计算机之间同步文件夹
  • 华为ICT和AI智能应用
  • ROS2与Unitree机器人集成指南
  • 在虚拟宇宙中低语——进程间通信,Linux命名管道的前世今生
  • Cursor 工具项目构建指南:Java 21 环境下的 Spring Boot Prompt Rules 约束
  • 各个布局的区别以及示例
  • 什么是MVC?
  • STM32的ADC简介
  • Bash shell四则运算
  • (javaSE)Java数组进阶:数组初始化 数组访问 数组中的jvm 空指针异常
  • 力扣刷题Day 70:在排序数组中查找元素的第一个和最后一个位置(34)
  • vue 多端适配之pxtorem
  • 图片压缩工具 | 图片属性详解及读取解析元数据
  • React---day8
  • C# Onnx 动漫人物人脸检测
  • C++内存列传之RAII宇宙:智能指针
  • PVE 虚拟机安装 Ubuntu Server V24 系统 —— 一步一步安装配置基于 Ubuntu Server 的 NodeJS 服务器详细实录1
  • GitHub 趋势日报 (2025年06月03日)
  • 出现dev/nvmeOnip2 contains a file system with errors, check forced 解决方法
  • Vue3.5 企业级管理系统实战(二十二):动态菜单
  • 磨皮功能 C++/C的OpenCV 实现
  • 蓝牙防丢器应用方案
  • TDengine 开发指南——高效写入