fetch格式
fetch('url'+参数a, { method: "GET", body: json } .then(res => response.json()) .then(console.log(json)) )
一个发送post请求的示例:
fetch("http://127.0.0.1:7777/postContent", {
method: "POST",
headers: {
"Content-Type": "application/json",
},
mode: "cors",
body: JSON.stringify({
content: "留言内容"
})
}).then(function(res) {
if (res.status === 200) {
return res.json()
} else {
return Promise.reject(res.json())
}
}).then(function(data) {
console.log(data);
}).catch(function(err) {
console.log(err);
});
fetch("/students.json")
.then(
function(response){
if(response.status!==200){
console.log("存在一个问题,状态码为:"+response.status);
return;
}
//检查响应文本
response.json().then(function(data){
console.log(data);
});
}
)
.catch(function(err){
console.log("Fetch错误:"+err);
});
- class AwesomeProject extends Component {// 初始化模拟数据
- constructor(props) {
- super(props);
- const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => {r1 !== r2}});
- this.state = {
- dataSource: ds,
- load:false,
- text:''
- };
- }
- //耗时操作放在这里面
- componentDidMount(){
- this.getNet();
- }
- getNet(){
- fetch('http://gank.io/api/search/query/listview/category/福利/count/10/page/1')//请求地址
- .then((response) => response.json())//取数据
- .then((responseText) => {//处理数据
- //通过setState()方法重新渲染界面
- this.setState({
- //改变加载ListView
- load: true,
- //设置数据源刷新界面
- dataSource: this.state.dataSource.cloneWithRows(responseText.results),
- })
- })
- .catch((error) => {
- console.warn(error);
- }).done();
- }
- render() {
- if(this.state.load){
- return (
- <View style={{flex: 1, paddingTop: 22}}>
- <ListView
- dataSource={this.state.dataSource}
- renderRow={(rowData)=>
- <View>
- <Image
- style={{ width: 400, height: 250, marginTop: 5 }}
- source={{uri:rowData.url}}/>
- </View>}
- />
- </View>
- );
- } else{
- return(
- <View>
- <Text>loading......</Text>
- </View>
- );
- }
- }
- }
项目:
export default class View extends Component {
state = {
total: {},
}
componentDidMount() {
fetch('')
.then((response) => {
return response.json();
})
.then((data) => {
this.setState({total: data.data});
});
}
render() {
const {total: {usersCount, msgsCount}} = this.state;
return (
<div className="user-summary">
<div className="summary-top">
<h1>微信公众号汇总</h1>
<ul>
<li>总关注人数:<span>{usersCount}</span></li>
<li>总消息数:<span>{msgsCount}</span></li>
</ul>
</div>
</div>
);
}
}
fetch格式的更多相关文章
- sql游标使用
一.游标的作用:Select时,返回的是一个结果集,若需要为结果集返回的过程中,读取到一行数据.需要对此行数据进行处理,比如按读取到的数据作为查询条件返回一个查询结果集等等,应用都需要用到游标.游标可 ...
- sql游标及模仿游标操作
游标用途:对一个查询出来的结果,每一行作为参数进行操作 一:游标操作 --申请一个游标 DECLARE MyCursor CURSOR FOR SELECT ID FROM dbo.tb_stock ...
- Git 少用 Pull 多用 Fetch 和 Merge
本文有点长而且有点乱,但就像Mark Twain Blaise Pascal的笑话里说的那样:我没有时间让它更短些.在Git的邮件列表里有很多关于本文的讨论,我会尽量把其中相关的观点列在下面. 我最常 ...
- 在 JS 中使用 fetch 更加高效地进行网络请求
在前端快速发展地过程中,为了契合更好的设计模式,产生了 fetch 框架,此文将简要介绍下 fetch 的基本使用. 我的源博客地址:http://blog.parryqiu.com/2016/03/ ...
- [No00003B]string格式的日期时间字符串转为DateTime类型
新建console程序,复制粘贴直接运行: /**/ //using System.Globalization;//代码测试大致时间2015/11/3 15:09:05 //方法一:Convert.T ...
- 【JSON 注解】JSON循环引用2----JSON注解@JsonIgnoreProperties+JAVA关键字transient+后台对象与JSON数据的格式互相转化
接着来说这个JSON循环引用的问题: 关于JSON格式的转化,其实关键就是这几个依赖: <!-- json --> <!-- 1号 --> <dependency> ...
- 关于Web项目里的给表单验证控件添加结束时间不得小于开始时间的验证方法,日期转换和前台显示格式之间,还有JSON取日期数据格式转换成标准日期格式的问题
项目里有些不同页面间的日期显示格式是不同的, 第一个问题: 比如我用日期控件WdatePicker.js导包后只需在input标签里加上onClick="WdatePicker()" ...
- (转)这个API很“迷人”——新的Fetch API
原文:https://hacks.mozilla.org/2015/03/this-api-is-so-fetching 原标题是This API is So Fetching,Fetching也可以 ...
- ES6 fetch函数与后台交互
最近在学习react-native,遇到调用后端接口的问题.看了看官方文档,推荐使用es6的fetch来与后端进行交互,在网上找了一些资料.在这里整理,方便以后查询. 1.RN官方文档中,可使用XML ...
随机推荐
- v2ex站长专访 - 100offer专访Livid:不仅仅是V站站长
转载自: https://www.douban.com/group/topic/121611313/ 前几天上网时偶然发现v2ex站长的blog(https://livid.v2ex.com/),了解 ...
- Oracle rownum的理解
核心过程分三步: 从表中取出行(无索引的话,顺序取出). 根据当前结果集,为当前行添加rownum. 条件筛选,如通过则添加到结果集中. 完.
- POJ:2586-Y2K Accounting Bug
Y2K Accounting Bug Time Limit: 1000MS Memory Limit: 65536K Description Accounting for Computer Machi ...
- 水题:CF16C-Monitor
Monitor 题目描述 Reca company makes monitors, the most popular of their models is AB999 with the screen ...
- 牛客练习赛29 B
炎热的早上,gal男神们被迫再操场上列队,gal男神们本来想排列成x∗x的正方形,可是因为操场太小了(也可能是gal男神太大了),校长安排gal男神们站成多个4∗4的正方形(gal男神们可以正好分成n ...
- poj 2385 树上掉苹果问题 dp算法
题意:有树1 树2 会掉苹果,奶牛去捡,只能移动w次,开始的时候在树1 问最多可以捡多少个苹果? 思路: dp[i][j]表示i分钟移动j次捡到苹果的最大值 实例分析 0,1 1,2...说明 偶数 ...
- ssh登陆之忽略known_hosts文件
在平时工作中,有时候需要SSH登陆到别的Linux主机上去,但有时候SSH登陆会被禁止,并弹出如下类似提示: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...
- HDU 5473 There was a kingdom 凸包 DP
题意: 给出平面上n个点的坐标,选k个点,使得这k个点围起来的面积最大. 分析: 参考了 叉姐的分析 和 不慌不忙菊苣的代码 思路我都懂,但是DP的部分还是不太会写. 我体会了一下其中含义,也许这样可 ...
- linux的vi和vim编辑器操作
vi:linux内部的文本编辑器:vim:vi的增强版,具有程序编辑的能力. vi和vim的三种常见模式: (1)正常模式(一般模式):vim一打开就是这种模式,此模式下可以使用各种快捷键,比如复制粘 ...
- 安装python包
开始--cmd pip install pymongo 直到最后给出提示successfull install pymongo-**(版本号,最新的)