简体   繁体   English

React Native Fetch JSON =网址为空

[英]React Native Fetch JSON = url empty

I'm developing an App (very simple App), that request a JSON API. 我正在开发一个应用程序(非常简单的应用程序),该应用程序需要JSON API。 The piece of code that makes me angry is : 令我生气的代码是:

fetch("http://xxx/api/v1/samples", {
  method: "GET",
  headers: {
    'Authorization': 'Bearer ' + userToken
  }
}).then((response) => {
  if (response.ok) {
    return response.json();
  }

  throw new Error('error');
}).then((samples) => {
  console.log(samples);
  this.setState({
    data: samples,
    error: samples.error || null,
    loading: false,
    refreshing: false
  });
}).catch((error) => {
  AlertIOS.alert("OOPS", error.message);
});

The API returns: API返回:

{
  "code": "4345",
  "id": 3,
  "comment": "Echantillon trouvé dans un étang",
  "updated_at": {
    "date": "2017-07-04 11:04:34.000000",
    "timezone_type": 3,
    "timezone": "Europe/Zurich"
  },
  "project_name": "Récolte de Lézards",
  "thumbs": "http://xxx/storage-app-uploads-public-595-e01-893-595e01893c826572100952-8cf6abfd3440f538dfa95b1d581b7487.png",
  "pictures": [
    "http://xxx/storage-app-uploads-public-595-e01-893-595e01893c826572100952-8cf6abfd3440f538dfa95b1d581b7487.png"
  ]
}

My problem The var "samples" is correct (contains all fields; id, code, comment, …) expect that all the urls are empty strings. 我的问题 var“样本”是正确的(包含所有字段; id,代码,注释等),期望所有url为空字符串。

The result of a console.log(samples) just before this.setState : { "code": "4345", "id": 3, "comment": "Echantillon trouvé dans un étang", "updated_at": { "date": "2017-07-04 11:04:34.000000", "timezone_type": 3, "timezone": "Europe/Zurich" }, "project_name": "Récolte de Lézards", "thumbs": "", "pictures": [ "" ] } this.setState之前的console.log(samples)的结果: { "code": "4345", "id": 3, "comment": "Echantillon trouvé dans un étang", "updated_at": { "date": "2017-07-04 11:04:34.000000", "timezone_type": 3, "timezone": "Europe/Zurich" }, "project_name": "Récolte de Lézards", "thumbs": "", "pictures": [ "" ] }

Cannot find out why. 找不到原因。

Any ideas? 有任何想法吗? Thank you so much. 非常感谢。

samples is a JavaScript plain object. samples是JavaScript的普通对象。 If expected result is an Array defined at "data" property of object passed to .setState() , set samples.pictures and .concat() samples.thumbs at "data" property. 如果预期的结果是一个Array ,在定义"data"传递到对象的属性.setState()samples.pictures.concat() samples.thumbs"data"属性。

Remove throw from .then() chained to fetch() call to avoid unexpected result. 从链接到fetch()调用的.then()删除throw .then()以避免意外结果。

data: samples.pictures.concat(samples.thumbs)

Thanks @YaSH, you are right. 谢谢@YaSH,您是对的。 With HTTPS url, the strings are not empty… But why… I did not find any mentions of that point in the documentation… 使用HTTPS url时,字符串不是空的...但是为什么...我在文档中没有提到这一点...

@guest271314: this is equal to the component in which the fetch is called. @ guest271314:这等于调用fetch的组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM