Reactjs之Axios、fetch-jsonp获取后台数据
1、新增知识点
/**
Axios获取服务器数据(无法跨域,只能让后台跨域获取数据)
react获取服务器APi接口的数据:
react中没有提供专门的请求数据的模块。但是我们可以使用任何第三方请求数据模块实现请求数据
axios介绍: https://github.com/axios/axios axios的作者觉得jsonp不太友好,推荐用CORS方式更为干净(后端运行跨域)
1、安装axios模块npm install axios --save / npm install axios --save
2、在哪里使用就在哪里引入import axios from 'axios'
3、看文档使用
var api='http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20';
axios.get(api)
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
}); 2、fetch-jsonp https://github.com/camsong/fetch-jsonp
fetch-jsonp请求:返回url会带回一个callback=
1、安装 npm install fetch-jsonp --save
2、import fetchJsonp from 'fetch-jsonp'
3、看文档使用
fetchJsonp('/users.jsonp')
.then(function(response) {
return response.json()
}).then(function(json) {
console.log('parsed json', json)
}).catch(function(ex) {
console.log('parsing failed', ex)
})
4、其他请求数据的方法也可以...自己封装模块用原生js实现数据请求也可以... 远程测试API接口:
get请求:
http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20
jsonp请求地址:
http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20&callback=?
*/
2、案例实现
class Home12 extends React.Component{
constructor(props){
console.log("组件加载,首先加载构造方法---1")
super(props);
this.state={
msg:"HOME12 组件信息",
list:[],
listjson:[]
}
}
componentWillMount() {
console.log("构造函数加载完成后,会加载componentWillMount(组件将要挂载)----2")
}
getData=()=>{
//通过axios获取数据
var api='http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20';
alert("获取数据");
axios.get(api).then((response)=> {
console.log(response.data.result); //接口返回数据
this.setState({
//用到this,要用到this取向
list:response.data.result
})
}).catch(function (error) {
console.log(error);//捕获异常数据
})
}
getfetchjsonpData=()=>{
//通过fetchjsonp获取数据
var api="http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20";
fetchJsonp(api)
.then(function(response) {
return response.json()//返回的json数据
}).then((json) =>{
//console.log('parsed json', json)
this.setState({
listjson:json.result
})
}).catch(function(ex) {
console.log('parsing failed', ex)
})
}
render() {
console.log("数据将要渲染---3")
return(
<div>
<h2>HOME12组件首页</h2>
<h1>axios获取数据</h1>
<button onClick={this.getData}>获取服务器api接口数据</button>
<hr/>
<ul>
{
this.state.list.map( (value,key) =>{
return(<li key={key}>{value.title}</li>)
})
}
</ul>
<hr/>
<h1>fetch-jsonp获取数据</h1>
<button onClick={this.getfetchjsonpData}>获取服务器api接口数据</button>
<hr/>
<ul>
{
this.state.listjson.map( (value,key) =>{
return(<li key={key}>{value.title}</li>)
})
}
</ul>
</div>
)
}
//生命的周期函数---组件加载完成
componentDidMount() {
console.log("组件加载完成---4")
this.getData();
}
}
Reactjs之Axios、fetch-jsonp获取后台数据的更多相关文章
- Vue axios异步获取后台数据alert提示undefined
记录一个小问题,关于分页查询套餐 前台通过axios异步请求获取后台数据alert弹出数据提示undefined 下面有三个bean PageResult /** * 分页结果封装对象 */ publ ...
- bootstrap table通过ajax获取后台数据展示在table
1. 背景 bootstrap table 默认向后台发送语法的dataType为 json,但是为了解决跨域问题我们需要将dataType改为jsonp,这时就需要修改bootstrap table ...
- 用ajax获取后台数据,返回json数据,怎么在前台使用?
用ajax获取后台数据,返回json数据,怎么在前台使用呢?后台 C# code ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 if (dataType == &qu ...
- Struts1.x下使用jquery的Ajax获取后台数据
jquery中有多种Ajax方法来获取后台数据,我使用的是$.get()方法,具体的理论我不解释太多,要解释也是从别的地方copy过来的.下面就介绍我的项目中的实现方法. 前台页面: ...
- Java获取后台数据,动态生成多行多列复选框
本例目标: 获取后台数据集合,将集合的某个字段,比如:姓名,以复选框形式显示在HTML页面 应用场景: 获取数据库的人员姓名,将其显示在页面,供多项选择 效果如下: 一.后台 查询数据库,返回List ...
- 前台通过ajax获取后台数据,PHP如何返回中文数据
现在经常使用Ajax调用后台php获取后台数据,但是PHP返回的数据如果含有中文的话,Ajax会无法识别,那咋整呢,我用的是比较笨的方法,但是实用: 方法一: echo urldecode(json_ ...
- jsonp获取服务器数据的方式
jsonp获取服务器的数据,有两种 一,跨域 二,不跨域 如果跨域 js的写法有两种 1, <script type="text/javascript"> $(func ...
- easyui panel异步获取后台数据在前台显示
我在使用easyui的时候,想做一个向下图所示的效果,这个panel的样式已经做好了,想从后台异步获取json数据,然后填入到文本框中,不知道哪位大神能给点指导?万分感谢! 放入表单中,使用form对 ...
- js 获取后台数据分页
页面创建一个存放内容的容器,以及分页的容器: <div id="content"></div> <div id="pager"&g ...
随机推荐
- Codeforces Round #593 (Div. 2) D. Alice and the Doll
题目:http://codeforces.com/problemset/problem/1236/D思路:机器人只能按照→↓←↑这个规律移动,所以在当前方向能够前进的最远处即为界限,到达最远处右转,并 ...
- 【Layui】当Layui数据表格和Layui下拉框组合时发生的问题
关于Layui数据表格用下拉框显示问题 如图所示 可以看见当点击下拉框时下拉选项被下拉框覆盖 此时你需要在数据表格渲染完成时的回调内添加如下代码即可 $(".sel_scrq"). ...
- 【leetcode】1234. Replace the Substring for Balanced String
题目如下: You are given a string containing only 4 kinds of characters 'Q', 'W', 'E' and 'R'. A string i ...
- Table表中数据类型的转换
各位大神帮我看下这还有别的方法转换类型吗? using System;using System.Collections.Generic;using System.Linq;using System.T ...
- c语言中"->"和"."的区别
对于c语言中"->"和"."的区别总结如下: 1.A.B则A为对象或者结构体: 2.A->B则A为指针,->是成员提取,A->B是提取A ...
- python-加密算法
#!/usr/bin/python3 # coding:utf-8 # Auther:AlphaPanda # Description: 使用hashlib模块的md5和sha系列加密算法对字符串进行 ...
- C# 父子窗体 传值
子窗体 public event Action<List<DataGridViewRow>> myEvent; myEvent(List); 子窗体调用 父窗体 创建对象 fr ...
- HyperSQL 链接参数中文件的路径
如果我们在系统中配置下面的连接参数: spring.datasource.url=jdbc:hsqldb:file:~/db/cwiki-us-jpetstore 我们怎么知道 hsqldb 数据库的 ...
- poj 2010 Moo University - Financial Aid 最大化中位数 二分搜索 以后需要慢慢体会
Moo University - Financial Aid Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6599 A ...
- Springboot 使用Jedis
Springboot 使用Jedis 依赖 <dependency> <groupId>org.springframework.boot</groupId> < ...