在《深入浅出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方法。

 

最新文章

  1. 【开源】分享2011-2015年全国城市历史天气数据库【Sqlite+C#访问程序】
  2. SQL语句性能测试
  3. 在cenOS下安装apache出现-bash: /etc/init.d/httpd: 没有那个文件或目录
  4. Inno Setup使用技巧
  5. python基本数据结构-集合-方法
  6. q和blockquote标签的区别
  7. Gliffy
  8. 手机端的各种默认样式比如 ios的按钮变灰色
  9. Android Studio 项目结构
  10. Andrew Ng机器学习课程笔记--week8(K-means&PCA)
  11. JVM菜鸟进阶高手之路四
  12. 项目实战——企业级Zabbix监控实战(一)
  13. 广告中的AdNetwork、AdExchange、DSP、SSP、RTB和DMP是什么?
  14. 借助HTML中的特殊符号在markdown中打印出来
  15. Sublime 黑科技之——lorem快速输入
  16. java获取request中的参数、java解析URL问号后的参数
  17. [转帖]SAP S/4 HANA与SAP Business Suite/R3(ECC)的区别
  18. 自学Python6.1-模块简介
  19. .NetCore发布到Centos docker
  20. 问答项目---金币经验奖励规则及网站配置写入config文件

热门文章

  1. Scala的Higher-Kinded类型
  2. 解决Idea配置文件不显示中文的问题
  3. HDU 5416 CBR and tree
  4. pvresize
  5. FluxInterval实例及解析
  6. 批量将制定文件夹下的全部Excel文件导入微软SQL数据库
  7. 关于 cmd 命令运行时发现错误(已加 classpath): 找不到或无法加载主类 xxx.class 原因: Java .lang.ClassNotFoundException: xxx.class
  8. Xapian实战(二):core concepts
  9. 在Jetson TX2上捕获、显示摄像头视频
  10. MySQL 基础入门