郑长生 發表於 2020-3-14 17:51:00

React网络请求

<div class="cnblogs_code">
<pre><span style="color: rgba(0, 128, 0, 1)">/*</span><span style="color: rgba(0, 128, 0, 1)">
react获取服务器APi接口的数据:


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


    1、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&amp;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


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


    3、其他请求数据的方法也可以...自己封装模块用原生js实现数据请求也可以...




远程测试API接口:


get请求:

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


jsonp请求地址:

    http://www.phonegap100.com/appapi.php?a=getPortalList&amp;catid=20&amp;callback=?



</span><span style="color: rgba(0, 128, 0, 1)">*/</span><span style="color: rgba(0, 0, 0, 1)">
import React, { Component } from </span>'react'<span style="color: rgba(0, 0, 0, 1)">;

import </span>'../assets/css/index.css'<span style="color: rgba(0, 0, 0, 1)">;

import Axios from </span>'./Axios'<span style="color: rgba(0, 0, 0, 1)">;


import FetchJsonp from </span>'./FetchJsonp'<span style="color: rgba(0, 0, 0, 1)">

class Home extends Component {

    constructor(props){
      super(props);      
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.state=<span style="color: rgba(0, 0, 0, 1)">{
          title:</span>'首页组件'<span style="color: rgba(0, 0, 0, 1)">         
      }
    }
    render() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (
      </span>&lt;div&gt;
         

            &lt;Axios /&gt;

            &lt;br /&gt;

            &lt;FetchJsonp /&gt;

      &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">      );
    }
}

export </span><span style="color: rgba(0, 0, 255, 1)">default</span><span style="color: rgba(0, 0, 0, 1)"> Home;
</span></pre>
</div>
<div class="cnblogs_code">
<pre>import React, { Component } from 'react'<span style="color: rgba(0, 0, 0, 1)">;


import axios from </span>'axios'<span style="color: rgba(0, 0, 0, 1)">;

class Axios extends Component {
    constructor(props) {
      super(props);
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {

            list:[]
         };
    }

    getData</span>=()=&gt;<span style="color: rgba(0, 0, 0, 1)">{


      </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">通过axios获取服务器数据</span>

      <span style="color: rgba(0, 0, 255, 1)">var</span> api='http://www.phonegap100.com/appapi.php?a=getPortalList&amp;catid=20';   <span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">接口后台允许了跨域</span>
<span style="color: rgba(0, 0, 0, 1)">
      axios.get(api)
      .then((response)</span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            console.log(response.data.result);

            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">用到this要注意this指向</span>

            <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({

                list:response.data.result

            })
      })
      .</span><span style="color: rgba(0, 0, 255, 1)">catch</span>(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)"> (error) {
            console.log(error);
      });


    }
    render() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (


            </span>&lt;div&gt;


                &lt;h2&gt;axios获取服务器数据&lt;/h2&gt;


                &lt;button onClick={<span style="color: rgba(0, 0, 255, 1)">this</span>.getData}&gt;获取服务器api接口数据&lt;/button&gt;

                &lt;hr /&gt;

                &lt;ul&gt;<span style="color: rgba(0, 0, 0, 1)">
                  
                  {

                        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.state.list.map((value,key)=&gt;<span style="color: rgba(0, 0, 0, 1)">{

                            </span><span style="color: rgba(0, 0, 255, 1)">return</span> &lt;li key={key}&gt;{value.title}&lt;/li&gt;
<span style="color: rgba(0, 0, 0, 1)">                        })
                  }   

                  
                </span>&lt;/ul&gt;


            &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">      );
    }
}

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> Axios;</pre>
</div>
<div class="cnblogs_code">
<pre>import React, { Component } from 'react'<span style="color: rgba(0, 0, 0, 1)">;


import fetchJsonp from </span>'fetch-jsonp'<span style="color: rgba(0, 0, 0, 1)">;


class FetchJsonp extends Component {
    constructor(props) {
      super(props);
      </span><span style="color: rgba(0, 0, 255, 1)">this</span>.state =<span style="color: rgba(0, 0, 0, 1)"> {

            list:[]
      };
    }

    getData</span>=()=&gt;<span style="color: rgba(0, 0, 0, 1)">{

         </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">获取数据</span>

      <span style="color: rgba(0, 0, 255, 1)">var</span> api="http://www.phonegap100.com/appapi.php?a=getPortalList&amp;catid=20"<span style="color: rgba(0, 0, 0, 1)">;
      fetchJsonp(api)
      .then(</span><span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(response) {
            </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> response.json()
      }).then((json)</span>=&gt;<span style="color: rgba(0, 0, 0, 1)"> {
            </span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)"> console.log(json);</span>
            
            <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)">.setState({

                list:json.result
            })

      }).</span><span style="color: rgba(0, 0, 255, 1)">catch</span>(<span style="color: rgba(0, 0, 255, 1)">function</span><span style="color: rgba(0, 0, 0, 1)">(ex) {
            console.log(</span>'parsing failed'<span style="color: rgba(0, 0, 0, 1)">, ex)
      })
    }
    render() {
      </span><span style="color: rgba(0, 0, 255, 1)">return</span><span style="color: rgba(0, 0, 0, 1)"> (

            </span>&lt;div&gt;


                &lt;h2&gt;FetchJsonp 获取服务器jsonp接口的数据&lt;/h2&gt;

                &lt;button onClick={<span style="color: rgba(0, 0, 255, 1)">this</span>.getData}&gt;获取服务器api接口数据&lt;/button&gt;

                &lt;hr /&gt;

                &lt;ul&gt;<span style="color: rgba(0, 0, 0, 1)">
                  
                  {

                        </span><span style="color: rgba(0, 0, 255, 1)">this</span>.state.list.map((value,key)=&gt;<span style="color: rgba(0, 0, 0, 1)">{

                            </span><span style="color: rgba(0, 0, 255, 1)">return</span> &lt;li key={key}&gt;{value.title}&lt;/li&gt;
<span style="color: rgba(0, 0, 0, 1)">                        })
                  }   

                  
                </span>&lt;/ul&gt;

            &lt;/div&gt;
<span style="color: rgba(0, 0, 0, 1)">            
      );
    }
}

export </span><span style="color: rgba(0, 0, 255, 1)">default</span> FetchJsonp;</pre>
</div>
<p>&nbsp;</p>

</div>
<div id="MySignature" role="contentinfo">
    最后,关注【码上加油站】微信公众号后,有疑惑有问题想加油的小伙伴可以码上加入社群,让我们一起码上加油吧!!!<br><br>
来源:https://www.cnblogs.com/loaderman/p/11556037.html
頁: [1]
查看完整版本: React网络请求