博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
fetch方法
阅读量:7071 次
发布时间:2019-06-28

本文共 931 字,大约阅读时间需要 3 分钟。

在《深入浅出React和Redux》一书中,介绍react与服务器端交互时,用了fetch方法:https://github.com/github/fetch。该网址上有各种使用例子。

安装:、

npm install whatwg-fetch --save

webpack上使用时:

entry: ['whatwg-fetch', ...]

For Babel and ES2015+:

import 'whatwg-fetch'

fetch方法现代浏览器或多或少原生支持,但是旧版不支持,所以需要下载promise fillpoly:https://github.com/taylorhakes/promise-polyfill。

npm install promise-polyfill --save-exact
1 import Promise from 'promise-polyfill'; 2 3 // To add to window4 if (!window.Promise) {5   window.Promise = Promise;6 }

基本使用方法如上。

不过看github上所说,也只支持到IE8+,足够了。

1 fetch('https://mywebsite.com/endpoint/', 2  { 3   method: 'POST',  4   headers: { 'Accept': 'application/json', 5         'Content-Type': 'application/json', },  6   body: JSON.stringify({ 7         firstParam: 'yourValue', 8         secondParam: 'yourOtherValue', })  9     })10 11 .then(function(res){12   console.log(res)13 })

只写一个url参数的话,就相当于一般的get方法。

 

 

 

 

转载于:https://www.cnblogs.com/alan2kat/p/7522746.html

你可能感兴趣的文章
十二月机房考核
查看>>
shell 类型
查看>>
网页中meta标记
查看>>
python爬虫笔记-day5
查看>>
Jenkins+newman 控制台输出样式
查看>>
公司业务转型,IT基础架构也要转型,京东云Docker容器集群微服务实践
查看>>
解释try_files $uri $uri/ /index.php$is_args$args;
查看>>
营销圈也可以提供类似“不涂口红的你”的创意文案?
查看>>
【源码分享】短信验证码功能对接CmsEasy
查看>>
学习linux入门之top命令的用法介绍
查看>>
MySQL的基础分部
查看>>
aix knowlgdgecenter
查看>>
好程序员分享JavaScript事件委托代理和函数封装详解
查看>>
VMWARE 占用硬盘空间只增大不减少的清理办法
查看>>
oracle技术之系统触发器的应用顺序(三)
查看>>
oracle RMAN备份FORMAT格式中%a的含义
查看>>
Oracle11gr2数据泵新特性(四)
查看>>
Oracle 11g r2数据泵新特性(一)
查看>>
我的友情链接
查看>>
iftop的安装及使用
查看>>