将FlatList与标头令牌一起使用

时间:2018-08-16 10:10:58

标签: reactjs native

我用Fetch构建了FlatList,该列表使用GET和随机用户生成器API进行操作。单元格会生成很好的数据。

但是,我使用了我实际上想使用的API来更改Fetch调用。该API更复杂,并且可以收集/显示更多数据。

这是我使用的代码。但是,它不输出任何内容。在控制台窗口上,单击“ if语句”后便不会输出日志。我在那里做错了什么?带有授权标头的POST API甚至可以与FlatList一起使用吗?

const myHeaders = new Headers({
    'Content-Type': 'application/json',
    'Accept': 'application/json',
    'Authorization': 'BASIC ' + this.state.savedToken,
});
var that = this;
var url = "https://AIlogistics.api.com/api/v1.json";
console.log("Test");
fetch(url,  { method: 'POST', headers: myHeaders, 
body: JSON.stringify({
    "date":"2018-08-11",
    "amount":"10",          
    "country":"CA",
    "program":"1000",
    "language":"ca-en",
    "device":"asdaksjdlka092821"
  })
}).then(function (response) {
    return response.json();
  }).then(function (result) {
    console.log("Test");

  if(result.data.status == 200){
    console.log("Test");

   that.setState({ 
     JSONResult: result.data,
   });
  }
console.log(result.data);
 }).catch(function (error) {
   console.log("-------- error ------- "+error);
  alert("result:"+error)
 });
}

0 个答案:

没有答案
相关问题