第11节 React获取服务器API接口数据2019-06-08

目前主要有两种方式:axios、fetch-jsonp

一、axios的使用

1.1安装

1.1.1使用NPM安装

npm install axios --save

1.1.2使用Yarn安装

$ yarn add axios

1.2引用

在哪里使用,在哪里引用

import Axios from 'axios'

1.3Axios示例

/**
 * 获取服务器数据
 */
getData=()=>{
    var api='http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20';
    axios.get(api)
        //.then(function (response) {
        .then((response)=> {
            console.log(response.data.result);
            this.setState({
                list:response.data.result
            })
        })
        .catch(function (error) {
            console.log(error);
        });
    //alert("获取服务器数据");
}

1.4DEMO示例

import  React from  'react';

import 'bootstrap/dist/css/bootstrap.min.css';

import axios from 'axios';

//定义组件
class Axios extends React.Component{
    constructor(props){
        super(props);
        this.state={
            list:[]
        }
    }

    /**
     * 获取服务器数据
     */
    getData=()=>{

        var api='http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20';

        axios.get(api)
            //.then(function (response) {
            .then((response)=> {
                console.log(response.data.result);
                this.setState({
                    list:response.data.result
                })
            })
            .catch(function (error) {
                console.log(error);
            });

        //alert("获取服务器数据");
    }

    render() {
        return (
            <div>
                <h2>axiox获取服务器数据</h2>
                <button onClick={this.getData}>获取</button>
                <hr/>
                <ul>
                    {
                        this.state.list.map((value,key)=>{
                           return <li key={key}>{value.title}</li>
                        })
                    }

                </ul>
            </div>
        )
    }
}

export default  Axios;//导出组件

1.5官网地址

https://www.npmjs.com/package/axios

二、fetch-jsonp的使用

2.1安装

npm install fetch-jsonp

2.2引用

import fetchJsonp from 'fetch-jsonp'

2.3fetch-jsonp示例

 getData=()=>{
        var api='http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20';

        fetchJsonp(api)
            .then(function(response) {
                return response.json()
            }).then((json)=> {
                this.setState({
                    list:json.result
                })
        }).catch(function(ex) {
            console.log('parsing failed', ex)
        })

    }

2.4完整DEMO示例

import  React from 'react';

import fetchJsonp from 'fetch-jsonp'
//定义组件
class Fecthjsonp extends React.Component{

    //构造函数
    constructor(props){
        super(props);

        this.state={
            list:[]
        }
    }

    getData=()=>{
        var api='http://www.phonegap100.com/appapi.php?a=getPortalList&catid=20';

        fetchJsonp(api)
            .then(function(response) {
                return response.json()
            }).then((json)=> {
                this.setState({
                    list:json.result
                })
        }).catch(function(ex) {
            console.log('parsing failed', ex)
        })

    }
    render() {
        return(
            <div>你好
              <h2></h2>
                <button onClick={this.getData}>获取数据</button>
                <ul>
                    {

                        this.state.list.map((value,key)=>{
                            return <li key={key}>{value.title}</li>
                        })
                    }

                </ul>
            </div>
        )
    }
}

export default  Fecthjsonp;//导出组件

2.5官网地址

https://www.npmjs.com/package/fetch-jsonp

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • ## 框架和库的区别?> 框架(framework):一套完整的软件设计架构和**解决方案**。> > 库(lib...
    Rui_bdad阅读 8,204评论 1 4
  • PS:转载请注明出处作者: TigerChain地址: https://www.jianshu.com/p/218...
    TigerChain阅读 26,489评论 5 70
  • http://npm.taobao.org/ https://reactjs.org/docs/getting-s...
    理子阅读 1,477评论 0 0
  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML标准。 注意:讲述HT...
    kismetajun阅读 28,496评论 1 45
  • 今天是你长这么大离开家里到别的城市生活,是值得记念的日子,我家小美女长大成人嫁为人妇两个半月以后要到贵州生活的日子...
    小木易杨阅读 1,616评论 0 0

友情链接更多精彩内容