react课堂笔记八之获取服务器数据 axios插件 fetch-jsonp插件的使用

react获取服务器APi接口的数据:

    react中没有提供专门的请求数据的模块。但是我们可以使用任何第三方请求数据模块实现请求数据

    1、axios          https://github.com/axios/axios       axios的作者觉得jsonp不太友好,推荐用CORS方式更为干净(后端运行跨域)
          1、安装axios模块npm install axios  --save   /  cnpm install axios  --save

          2、在哪里使用就在哪里引入import axios from 'axios

          3、看文档使用

            axios.get('/users.axios')
            .then(function (response) {
                console.log(response);
            })
            .catch(function (error) {
                console.log(error);
            });

    2、fetch-jsonp    https://github.com/camsong/fetch-jsonp

            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)
            })
 

扫描二维码关注公众号,回复: 5277295 查看本文章

猜你喜欢

转载自blog.csdn.net/weixin_39247773/article/details/87860716