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 ...
随机推荐
- 【Python学习之七】面向对象高级编程——__slots__的使用
1.Python中的属性和方法的绑定 正常情况下,当我们定义了一个class,创建了一个class的实例后,我们可以给该实例绑定任何属性和方法. (1)首先,定义一个class: class Stu ...
- cURL error 60: SSL certificate problem: unable to get local issuer certificate (see http://curl.haxx.se/libcurl/c/libcurl-errors.html)
参考 http://blog.csdn.net/mazicwong/article/details/54946952 1.到https://curl.haxx.se/ca/cacert.pem复制下 ...
- ultraedit编辑器破解版下载
ultraedit一款功能丰富的网站建设软件,需要的朋友可以看看. 百度百科:UltraEdit 是一套功能强大的文本编辑器,可以编辑文本.十六进制.ASCII 码,完全可以取代记事本(如果电脑配置足 ...
- OOP之单例模式
- spring+struts2+mybatis框架依赖pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- CodeForce--Benches
A. Benches There are nn benches in the Berland Central park. It is known that aiai people are curr ...
- HDU 1533 二分图最小权匹配 Going Home
带权二分图匹配,把距离当做权值,因为是最小匹配,所以把距离的相反数当做权值求最大匹配. 最后再把答案取一下反即可. #include <iostream> #include <cst ...
- 启动activity的标准的action常量及对应的字符串
- 关于windows服务的编写/安装/与调试
前注: 首先,这篇文章是从网上转过来的,因为最近有个项目,需要编写一个Windows Service来定时执行程序,网上很容易找到了这篇文章,大概看了一下,文章讲的还是很详细的.不过这篇文章应该是.n ...
- __block 和__weak
1,在MRC 时代,__block 修饰,可以避免循环引用:ARC时代,__block 修饰,同样会引起循环引用问题: 2,__block不管是ARC还是MRC模式下都可以使用,可以修饰对象,还可以修 ...