fetch方法
2024-10-09 04:02:50
在《深入浅出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
import Promise from 'promise-polyfill'; // To add to window
if (!window.Promise) {
window.Promise = Promise;
}
基本使用方法如上。
不过看github上所说,也只支持到IE8+,足够了。
fetch('https://mywebsite.com/endpoint/',
{
method: 'POST',
headers: { 'Accept': 'application/json',
'Content-Type': 'application/json', },
body: JSON.stringify({
firstParam: 'yourValue',
secondParam: 'yourOtherValue', })
}) .then(function(res){
console.log(res)
})
只写一个url参数的话,就相当于一般的get方法。
最新文章
- 【开源】分享2011-2015年全国城市历史天气数据库【Sqlite+C#访问程序】
- SQL语句性能测试
- 在cenOS下安装apache出现-bash: /etc/init.d/httpd: 没有那个文件或目录
- Inno Setup使用技巧
- python基本数据结构-集合-方法
- q和blockquote标签的区别
- Gliffy
- 手机端的各种默认样式比如 ios的按钮变灰色
- Android Studio 项目结构
- Andrew Ng机器学习课程笔记--week8(K-means&;PCA)
- JVM菜鸟进阶高手之路四
- 项目实战——企业级Zabbix监控实战(一)
- 广告中的AdNetwork、AdExchange、DSP、SSP、RTB和DMP是什么?
- 借助HTML中的特殊符号在markdown中打印出来
- Sublime 黑科技之——lorem快速输入
- java获取request中的参数、java解析URL问号后的参数
- [转帖]SAP S/4 HANA与SAP Business Suite/R3(ECC)的区别
- 自学Python6.1-模块简介
- .NetCore发布到Centos docker
- 问答项目---金币经验奖励规则及网站配置写入config文件
热门文章
- Scala的Higher-Kinded类型
- 解决Idea配置文件不显示中文的问题
- HDU 5416 CBR and tree
- pvresize
- FluxInterval实例及解析
- 批量将制定文件夹下的全部Excel文件导入微软SQL数据库
- 关于 cmd 命令运行时发现错误(已加 classpath): 找不到或无法加载主类 xxx.class 原因: Java .lang.ClassNotFoundException: xxx.class
- Xapian实战(二):core concepts
- 在Jetson TX2上捕获、显示摄像头视频
- MySQL 基础入门