ajax()

import React from 'react';
import ReactDom from 'react-dom';
import ajax from './tool.js'; class Nav extends React.Component{
constructor(){
super();
this.state = {
arr: {}
};
this.get = this.get.bind(this);
}
get(){
ajax('./data/data.json',function(res){
// var json = JSON.parse(res);
var json = (new Function('return' + res))();
console.log(json);
})
}
render(){
return <div>
<input type="button" value="按钮" onClick={this.get}/>
</div>
}
} ReactDom.render(
<Nav></Nav>,
document.querySelector('#app')
);

$.ajax()

import React from 'react';
import ReactDom from 'react-dom';
import $ from 'jquery'; class Nav extends React.Component{
constructor(){
super();
this.state = {
arr: {}
};
this.get = this.get.bind(this);
}
get(){
$.ajax({
type:'get',
url:'data/data.json',
success:function(res){
console.log(res);
}
})
}
render(){
return <div>
<input type="button" value="按钮" onClick={this.get}/>
</div>
}
} ReactDom.render(
<Nav></Nav>,
document.querySelector('#app')
);

fetch()------------------fetch在ios10.x.x可能会存在问题

(大概是10.2.x??记不清了,衰~是在做微信公众号开发时候遇到的问题~不排除是x5内核所致啊,总之当初把fetch换成axios就好了)。

import React from 'react';
import ReactDom from 'react-dom';
import $ from 'jquery'; class Nav extends React.Component{
constructor(){
super();
this.state = {
arr: {}
};
this.get = this.get.bind(this);
}
get(){
fetch('data/word.txt').then((res)=>{
if(res.ok){
res.text().then((data)=>{
console.log(data);
})
}
}).catch((res)=>{
console.log(res.status);
});
}
render(){
return <div>
<input type="button" value="按钮" onClick={this.get}/>
</div>
}
} ReactDom.render(
<Nav></Nav>,
document.querySelector('#app')
);

第四种:axios

import React from 'react';
import ReactDom from 'react-dom';
import axios from 'axios'; class Nav extends React.Component{
constructor(){
super();
this.state = {
arr: {}
};
this.get = this.get.bind(this);
}
get(){
axios.get('./data/data.json').then((res)=>{
console.log(res.data);
console.log(res.data[3]);
}).catch((err)=>{
console.log(err.status);
})
}
render(){
return <div>
<input type="button" value="按钮" onClick={this.get}/>
</div>
}
} ReactDom.render(
<Nav></Nav>,
document.querySelector('#app')
);

react——获取数据ajax()、$.ajax()、fetch()、axios的更多相关文章

  1. PDO获取数据的方法fetch()、fetchAll()、setFetchMode()、bindColumn()

    PDO的数据获取方法与其他数据库扩展都非常类似,只要成功执行SELECT查询,都会有结果集对象产生.不管是使用PDO对象中的query()方法,还是使用prepare()和execute()等方法结合 ...

  2. Swift - 后台获取数据(Background Fetch)的实现

    前面讲了如何让程序申请后台短时运行.但这个额外延长的时间毕竟有限.所以从iOS7起又引入两种在后台运行任务的方式:后台获取和后台通知. 1,后台获取介绍 后台获取(Background Fetch)是 ...

  3. 三种获取数据的方法fetch和ajax和axios

    一 .fetch用法 ( 本人比较喜欢fetch,代码精简,虽说目前axios比较流行,但是fetch很多大厂已经开始用fetch开始封装了, 我觉得以后fetch会取代axios和ajax ) 1. ...

  4. Ajax从服务器端获取数据---原生态Ajax

    写在前面的话 Ajax从服务器获取的数据都是字符串,但是通过不同的解析,可以解析为XML或JSON来进行应用. 一般来说.使用XML格式的数据比较通用,但是服务器和客户端解析起来都比较复杂一些;而使用 ...

  5. 通过 HTTP 请求加载远程数据(ajax,axios)

    1.http://blog.csdn.net/liaoxiaojuan233/article/details/54176798 (Axios(JS HTTP库/Ajax库)) 2.https://ww ...

  6. React获取数据,假如为数组,使用map出现的问题

    在平时做项目的时候,使用到了redux, 如果获取服务器端的数据,例如返回一个  data = [1,2,3,4]data.map(item => item*2) , 这样使用的话如果数据正常获 ...

  7. MVC—实现ajax+mvc异步获取数据

    之前写过ajax和一般处理程序的结合实现前后台的数据交换的博客,如今做系统用到了MVC,同一时候也用到了异步获取数据. ajax+一般处理程序与MVC+ajax原理是一样的在"URL&quo ...

  8. 05 . Vue前端交互,fetch,axios,以asyncawait方式调用接口使用及案例

    目标 /* 1. 说出什么是前后端交互模式 2. 说出Promise的相关概念和用法 3. 使用fetch进行接口调用 4. 使用axios进行接口调用 5. 使用asynnc/await方式调用接口 ...

  9. React 中的 AJAX 请求:获取数据的方法

    React 中的 AJAX 请求:获取数据的方法 React 只是使用 props 和 state 两处的数据进行组件渲染. 因此,想要使用来自服务端的数据,必须将数据放入组件的 props 或 st ...

随机推荐

  1. request的基本应用

    一.安装 pip install requests (mac前面加sudo) 二.requests的一些参数 method:一般是用的那种请求方法,是get还是post,delete或者delete ...

  2. Codeforce 886 Технокубок 2018 - Отборочный Раунд 3 C. Petya and Catacombs(结论题)

    A very brave explorer Petya once decided to explore Paris catacombs. Since Petya is not really exper ...

  3. Android MVP 架构一 View与Presenter

    View:主要负责界面的显示及跟数据无关的逻辑,比如设置控件的点击事件等 Presenter:主要负责View与Model的交互 Model:数据部分 ------- MVP的核心是: View层不持 ...

  4. Httpclient超时timeout设置

    一:连接超时:connectionTimeout 1:指的是连接一个url的连接等待时间. 二:读取数据超时:soTimeout 1:指的是连接上一个url,获取response的返回等待时间. Fo ...

  5. node.js(小案例)_使用mongodb对学生信息列表优化

    一.前言 1.这篇文章主要对上一篇案列在操作增删改的时候使用mongodb进行优化 2.项目源码(包含上):https://github.com/4561231/crud-express-node.g ...

  6. mysql如何从全备文件中恢复单个库或者单个表

    mysql如何从全备文件中恢复单个库或者单个表 在mysql dba的日常实际工作中,一个实例下有多个库,而我们常见的备份就是全库备份.那么问题就来了,如果需要恢复单个库或者单个表,怎么办了,网上有很 ...

  7. 图论分支-Tarjan初步-边双联通分量

    本来应该先说强连通分量,但是有一定的分配,所以这个在下一篇博客将会见到. 这个本想连点连通分量一起讲,但是量有点大,所以我就分两步讲. 我们先看定义 再来看看图解 很容易就能发现,只要将割边断掉,然后 ...

  8. jvm垃圾收集器与内存分配策略

    一.垃圾回收 1.对象是否已经变为垃圾 1.1.引用计数法:给对象添加一个引用计数器,每当有地方引用它时,计数器就加1:当引用失效时,计数器值就减1:任何时刻计数器为0的对象就是不可能再被使用的. 这 ...

  9. git lg 使用 转

    命令: git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yello ...

  10. Hbase 1.3.0 Rsgroup

    HBase RSGroup Git环境window环境下,警用crlf自动转换git config --global core.autocrlf false protobuf环境yum install ...