react系列(7)ajax网络请求
来源:互联网 发布:淘宝秒杀需要登录吗 编辑:程序博客网 时间:2024/06/04 00:22
网络请求通常发生在组件刚完成挂载componentDidMount的时候,只要遵照jquery的ajax标准写法标准即可。如下例中的get请求。
var AjaxTest = React.createClass({getInitialState:function(){return {name : "null",gisurl:"null"};},componentDidMount:function(){var target = this;//写法一this.serverRequest = $.ajax({url: this.props.source,data: "",type: 'GET',timeout: 30000,error: function(response, state) {console.log(response);},success: function(response, state) { var lastGist = response[0]; target.setState({ name: lastGist.owner.login, gisurl: lastGist.html_url });}});//写法二//this.serverRequest = $.get(this.props.source, function (result) {// var lastGist = result[0];// this.setState({// name: lastGist.owner.login,// gisurl: lastGist.html_url// });// }.bind(this));},componentWillUnmount: function() { this.serverRequest.abort(); //在组件卸载前取消请求},render:function(){return (<div><h1>"name:"+{this.state.name}</h1><h1>"gisurl:"+{this.state.gisurl}</h1></div>);}});ReactDOM.render(<AjaxTest source="https://api.github.com/users/octocat/gists" />,document.getElementById("example"));
(注)在组件卸载前如请求仍未结束,一定要取消请求。
阅读全文
0 0
- react系列(7)ajax网络请求
- React Native网络请求
- React Native 网络请求
- React Native网络请求(Frisbee网络框架)
- react.js-11-网络请求
- React-Native中网络请求
- React-native fetch请求网络
- React Native Fetch网络请求
- ajax异步网络请求
- React-Native进行时(三)--JS网络请求解析
- React-Native 学习笔记十七(网络请求)
- React Native 学习笔记(九)--网络请求 & 界面跳转
- React Native从零开始(七)Fetch网络请求
- React Native探索(五)使用fetch进行网络请求
- React Native入门(八)之网络请求Fetch
- 微信小程序系列4(网络请求)
- 如何在React中做Ajax 请求?
- ReactJS学习系列课程(React Http网络数据读取)
- selenium使用代理
- iOS后台语音播报
- java --assert关键字断言
- JS——点击放大图片或鼠标滚动方法图片(大小图对应选择)
- Java 跨域 Json字符转类对象
- react系列(7)ajax网络请求
- 写给大数据开发初学者的话
- 使用Eclipse中的egit将代码提交到码云
- VMWARE DHCP SERVICE服务启动时报错:错误1068 依存服务或组无法启动
- MySQL数据库优化的八种方式(经典必看)
- C++继承
- souce command not found?
- 图片二值化
- [leetcode]#141. Linked List Cycle