I am learning React.js and trying to fetch API like this
JavaScript
x
32
32
1
constructor() {
2
super();
3
4
this.state = {person: []};
5
}
6
7
componentDidMount() {
8
fetch('https://randomuser.me/api/?results=500', {mode: 'no-cors'})
9
.then(results => {
10
return results.json();
11
})
12
.then(data => {
13
let person = (data.results || []).map((pic) => {
14
return(
15
<div key={pic.results}></div>
16
)
17
})
18
19
this.setState({person: person});
20
console.log("state", this.state.person);
21
})
22
}
23
24
render() {
25
26
return (
27
<div>
28
{this.state.person}
29
</div>
30
);
31
}
32
and this is the error I get
I am pretty new in this, so if anyone can help me with this that would be great. Thanks
Advertisement
Answer
Try by setting the header as follows. https://developer.mozilla.org/en-US/docs/Web/HTTP/CORS You just remove the mode: ‘no-cors’ in your code,
JavaScript
1
82
82
1
componentDidMount() {
2
fetch('https://randomuser.me/api/?results=500')
3
.then(response => response.json())
4
.then(resData => {
5
//console.log(JSON.stringify(resData))
6
//do your logic here
7
//let person = resData.results
8
this.setState({ person: resData.results }); //this is an asynchronous function
9
})
10
}
11
12
render(){
13
14
return(
15
<div>
16
{
17
this.state.person.map((personRecord) => {
18
return <div key={personRecord.id.value}> {personRecord.name.first} </div>
19
})
20
}
21
</div>
22
)
23
}
24
25
//sample result json
26
{
27
"results": [ //we already fetched the results array of object from resData
28
{
29
"gender": "female",
30
"name": {
31
"title": "mrs",
32
"first": "astrid",
33
"last": "jørgensen"
34
},
35
"location": {
36
"street": "2675 strandgårdsvej",
37
"city": "hurup thy",
38
"state": "danmark",
39
"postcode": 63288,
40
"coordinates": {
41
"latitude": "-27.6956",
42
"longitude": "104.8135"
43
},
44
"timezone": {
45
"offset": "+5:30",
46
"description": "Bombay, Calcutta, Madras, New Delhi"
47
}
48
},
49
"email": "astrid.jørgensen@example.com",
50
"login": {
51
"uuid": "25ab4c50-9a8c-48bc-a276-acae2209aa19",
52
"username": "happymouse810",
53
"password": "journey",
54
"salt": "OGXHTU6k",
55
"md5": "5c659a3d97b081fc18f0bf94f246751d",
56
"sha1": "407020d4230121788180244775edd6fbea56c375",
57
"sha256": "0cc94ec5d89f71903c9f74dcd12253240b1269e75a3ca67eae3f4d578e47d3f8"
58
},
59
"dob": {
60
"date": "1978-03-28T17:31:08Z",
61
"age": 40
62
},
63
"registered": {
64
"date": "2017-04-19T14:15:02Z",
65
"age": 1
66
},
67
"phone": "10566067",
68
"cell": "24745172",
69
"id": {
70
"name": "CPR",
71
"value": "295410-3587"
72
},
73
"picture": {
74
"large": "https://randomuser.me/api/portraits/women/12.jpg",
75
"medium": "https://randomuser.me/api/portraits/med/women/12.jpg",
76
"thumbnail": "https://randomuser.me/api/portraits/thumb/women/12.jpg"
77
},
78
"nat": "DK"
79
}
80
]
81
}
82
JavaScript
1
4
1
//By using this way we can able to get the result, but it just delays
2
fetch('https://randomuser.me/api/?results=500')
3
.then(response => response.json())
4
.then(resData => console.log(resData))