美文网首页
React native - Fetch网络请求

React native - Fetch网络请求

作者: 饭后的甜点_ | 来源:发表于2017-11-22 16:55 被阅读30次
  1. get请求
fetch('https://mywebsite.com/endpoint/', {
  method: 'GET',  // 请求方式   有GET POST  
  headers: {
    'Accept': 'application/json',   //希望接收的格式
    'Content-Type': 'application/json',  //发送的格式  一般都为JSON 
  },
})
 .then((response) => response.json())  //  接收到数据后 序列号 也可text text既没有格式
 .catch((error) => {
        console.error(error);
 });  //请求失败 

get 请求一般就是 在一个域名 + 请求action + ? + 参数 参数之间用&连接
ex:
https://huodong.taobao.com/wow/tb-act-new/ (*** action -->) act/meiyan?spm=a21bo.2017.201862-1.d1.67aef70eSbQFBz&pos=1&acm=20140506001.1003.2.2546600&scm=1003.2.20140506001.OTHER_1510588319083_2546600

2.post 请求

fetch('https://mywebsite.com/endpoint/', {
  method: 'POST',
  headers: {
    'Accept': 'application/json',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    firstParam: 'yourValue',          //参数放到body中不拼接在网址后  转换为JSON格式
    secondParam: 'yourOtherValue',
  })
})
 .then((response) => response.json())  //  接收到数据后 序列号 也可text text既没有格式
 .catch((error) => {
        console.error(error);
 });  //请求失败 

post 请求不同于get 一般情况参数不拼接在 网址后面
我们将参数 转换为JSON格式 放到body中 反序列化

相关文章

网友评论

      本文标题:React native - Fetch网络请求

      本文链接:https://www.haomeiwen.com/subject/tykovxtx.html