react-native(Fetch网络请求数据)

语法使用:

fetch(参数)
.then(完成的回调函数)
.catch(失败的回调函数)

fetch(url,opts)
.then((response) => {
//请求成功后返回的对象response
//例如:json、text等
return response.text();
return response.json();
})
.then((responseData) => {
//处理请求得到的数据
})
.catch((error) => {
//网络请求失败执行该回调函数,得到错误信息
})

语法很简单,感觉类似于Android新建Notification一样。
then第一个是获取整个数据,第二个是根据第一个数据转换而来的你所需要的数据,也就是说你需要一个json数据那么在第一步返回json就好。

1、GET请求

getRequest(url){  
      /*网络请求的配置*/  
      var opts = {  
          method:"GET"  
      }  
      fetch(url,opts)  
          .then((response) => {  
              return response.text();  
          })  
          .then((responseText) => {  
              alert(responseText);  
          })  
          .catch((error) =>{  
              alert(error);  
          })  
  }  

2、POST请求

postRequest(url){  
       let formData = new FormData();  
       formData.append("username","SuperBigLw");  
       formData.append("password","123456");  
       var opts = {  
           method:"POST",  
           body:formData  
       }  
       fetch(url,opts)  
           .then((response) => {  
               return response.text();  
           })  
           .then
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值