Posted on:
Last modified:
fetch 是近年来浏览器实现的一个用于取代 xhr 的 API,相比于 xhr 来说更加简单易用安全且强大。 主要区别有:
const response = await fetch("http://example.com/movies.json");
const myJson = await response.json(); // text() 返回纯文本
console.log(JSON.stringify(myJson));
特别注意的是,在 Flask 等后端框架中,如果不添加 application/json
的 header, 默认不会
解析 json, 太坑了。默认情况下 fetch 是会携带同一个域名下的 Cookie 的
try {
// Default options are marked with *
const response = await fetch(url, {
method: "POST", // *GET, POST, PUT, DELETE, etc.
mode: "cors", // no-cors, *cors, same-origin
cache: "no-cache", // *default, no-cache, reload, force-cache, only-if-cached
credentials: "same-origin", // include, *same-origin, omit
headers: {
"Content-Type": "application/json"
// "Content-Type": "application/x-www-form-urlencoded",
},
redirect: "follow", // manual, *follow, error
referrer: "no-referrer", // no-referrer, *client
body: JSON.stringify(data) // body data type must match "Content-Type" header
});
console.log(await response.json());
} catch (error) {
console.error(error);
}
// 注意这个其实挺坑的,必须是一个完整的 url,而不能使用一个片段
let url = new URL('https://sl.se')
let params = {lat:35.696233, long:139.570431} // or:
let params = [['lat', '35.696233'], ['long', '139.570431']]
url.search = new URLSearchParams(params).toString();
fetch(url)
在 Node 中可能需要:import {URL, URLSearchParams} from 'url'
fetch 的 credentials 有三个选项:
omit
不携带任何 cookiesinclude
携带所有 cookiessame-origin
只有向当前网站的同源域名发送请求时才携带 cookies其中 same-origin
是默认选项。
const headers = new Headers();
headers.append("Content-Type", "text/plain");
headers.append("Content-Length", content.length.toString());
headers.append("X-Custom-Header", "ProcessThisImmediately");
// 另一种方法是直接使用字典
const headers = new Headers({
"Content-Type": "text/plain",
})
const init = {
method: "POST",
headers: headers,
mode: "cors",
cache: "default",
}
const request = new Request("flowers.jpg", init);
const rsp = await fetch(request);
© 2016-2022 Yifei Kong. Powered by ynotes
All contents are under the CC-BY-NC-SA license, if not otherwise specified.
Opinions expressed here are solely my own and do not express the views or opinions of my employer.
友情链接: MySQL 教程站