fetch

  • fetch api
  • 文档地址
  • 模拟登录demo

fetch api

fetch api 是浏览器的异步可跨域请求。基于XMLHttpRequest, 也就是对原生Ajax请求的包装,以回调的形式展开。

使用方法:

fetch('http://example.com/movies.json').then(function(response) {return response.json();}).then(function(myJson) {console.log(myJson);});

文档地址

https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetch

模拟登录demo

<!DOCTYPE html>
<html lang="en"><head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>Document</title>
</head><body style="width: 100%; height: 100%;"><div><img src="" /><br /><label></label><br /><textarea id="token" style="width: 400px; height: 60px;"></textarea></div><div><form id="loginForm" name="loginForm">code: <input name="code" /> <br />username: <input name="username" /> <br />password: <input name="password" type="password" /> <br />uuid: <input name="uuid" /> <br /><input type="button" onclick="doLogin()" value="login" /></form></div><script>// https://developer.mozilla.org/zh-CN/docs/Web/API/Fetch_API/Using_Fetchfunction doLogin() {console.log('start login..')let form = document.getElementById("loginForm");console.log(form.code.value)let code = form.code.value;let uuid = form.uuid.value;let username = form.username.value;let password = form.password.value;let data = {uuid, code, username, password }var req = new Request("http://localhost:9999/login")var init = {method: 'POST',mode: 'cors',body: JSON.stringify(data),headers: {'content-type': 'application/json'}}fetch(req, init).then(res => res.json()).then(res => {console.log(res)document.getElementById("token").innerText = res.token;})}var myImage = document.querySelector('img');var myHeaders = new Headers();myHeaders.append('Content-Type', 'image/jpeg');var myInit = {method: 'GET',headers: myHeaders,mode: 'cors',cache: 'default'};// 获取验证码var myRequest = new Request('http://localhost:9999/captchaImage');fetch(myRequest, myInit).then(function (response) {return response;}).then(res => {return res.json();}).then(res => {console.log(res)myImage.src = 'data:image/png;base64,' + res.img;document.querySelector("label").innerText = res.uuid;});</script>
</body></html>

关于fetch api这点事相关推荐

  1. 深入浅出Fetch API

    多年来,XMLHttpRequest一直是web开发者的亲密助手.无论是直接的,还是间接的, 当我们谈及Ajax技术的时候,通常意思就是基于XMLHttpRequest的Ajax,它是一种能够有效改进 ...

  2. 如何使用浏览器对网络请求编辑重发(Edit and Resent) 以及: fetch API 中 ReadablleStream 的打印方法

    文章目录 需求 火狐浏览器-编辑重发(Edit and Resent) fetch API Links 需求 上网用F12分析一个请求,调用的事服务端的一个数据接口. 想修改请求参数(请求头.请求体, ...

  3. 使用JavaScript Fetch API发送HTTP请求

    介绍 JavaScript的Fetch API允许我们发送HTTP请求.自从ECMAScript 2015(通常称为ES6)被引入并使用Promises以来,它一直是JavaScript的标准部分. ...

  4. Fetch API 初步解读

    文 | Leigh,UPYUN 已获得授权 微信文章链接:http://t.cn/R4afStO 在我们日常的前端开发中,XMLHttpRequest 是必不可少会遇到的一个东东.XHR 最初是由微软 ...

  5. Fetch API HTTP请求实用指南

    本次将介绍如何使用Fetch API(ES6 +)对REST API的 HTTP请求,还有一些示例提供给大家便于大家理解. 注意:所有示例均在带有箭头功能的 ES6中给出. 当前的Web /移动应用程 ...

  6. Fetch API——简化你的AJAX

    之前搞定了Promise,接下来学习Fetch API.封装过AJAX函数的各位肯定能知道XMLHttpRequest的问题(没有封装过的点这里),首先,其所有的功能全部集中在同一对象上,容易书写混乱 ...

  7. 什么是 Web API 中的 background fetch API

    在为 Web 编写代码时,有大量可用的 Web API.这个网站列出了您在开发 Web 应用程序或站点时可以使用的所有 API 和接口(对象类型). Web API 通常与 JavaScript 一起 ...

  8. ES6 Fetch API和Cookie相关的知识点

    Jerry Wang曾经写过一篇比较web应用处于stateful和stateless不同状态下的行为差异,发表在SAP社区上: https://blogs.sap.com/2017/03/31/st ...

  9. 使用fetch封装请求_关于如何使用Fetch API执行HTTP请求的实用ES6指南

    使用fetch封装请求 In this guide, I'll show you how to use the Fetch API (ES6+) to perform HTTP requests to ...

最新文章

  1. 三种序列化方式性能比较
  2. 2021牛客暑期多校训练营3 I-Kuriyama Mirai and Exclusive Or (差分+位运算)
  3. python长度分割文本_python 按照固定长度分割字符串的方法小结
  4. 个人博客多说评论系统的使用
  5. leetcode --Minimum Depth of Binary Tree
  6. linux 文件目录操作,Linux系统下文件与目录操作
  7. mysql 打包表在phpmyadmin提示正在使用中..
  8. Docker文件系统实战
  9. 设计模式 - 命令模式(command pattern) 撤销(undo) 具体解释
  10. uva 12108 Extraordinarily Tired Students(特别困的学生)
  11. HTML5中本地储存概念是什么,什么优点 ,与cookie有什么区别?
  12. [转] Batch Normalization
  13. html静态页面留言板,html静态留言板
  14. 《从零走向专业,面试产品经理岗位必须掌握的7个高效方法》
  15. 终极单词index 排序 M-N
  16. 中国智慧消防产业需求现状及十四五发展趋向分析报告2021-2027年版
  17. python 将微信聊天记录生成词云
  18. js 浏览器永久保存数据:localStorage
  19. google手机连接Wifi后总会提示无法连接互联网问题的解决
  20. java long to int_java int 转 Long

热门文章

  1. 前端学习(1744):前端调试值之调试元素的盒模型
  2. 前端学习(1652):前端系列实战课程之bom
  3. 前端学习(1390):多人管理项目10服务器认证
  4. 前端学习(570):margin负值下的等高布局
  5. java面试题33 Math.round(11.5) 等于多少 (). Math.round(-11.5) 等于多少 ( ).
  6. 玩转oracle 11g(11):开启归档模式
  7. CSS之Multi-columns的column-gap和column-rule
  8. Jupyter notebook 入门教程
  9. JMeter - 如何创建可重用和模块化测试脚本
  10. PHP扩展开发(3)-config.m4