My JSX file returns the API data when accessed from myjson.com; but when I attempt to access the data from the provided URL for the API, I get an "Unhandled Rejection -SyntaxError- Unexpected end of JSON input" error. The provided URL, when placed into Postman, returns data via GET when a Bearer Token is provided.
My question: does that same token have to be implemented within my JSX file in order to access the data for the API and if so, how is that implemented within the code?
This is my Code (modified):
import React, { Component } from 'react';
import {render} from "react-dom";
import Navbar from '../components/Navbar.jsx';
import Footer from '../components/Footer.jsx';
import './MemberInfo.css';
class MemberInfo extends Component {
state = { data: [] }
componentWillMount(){
fetch('http://myjson.com/api/e312j/', {
method: 'GET',
headers: {
'Accept': 'application/json',
'Content-type': 'application/json',
'Authorization': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1lIjoiR3JlZyIsInVuaXF1ZV9uYW1lIjoiZ2dyYWZmIiwibmJmIjoxNTI0ODM5Nzc1LCJleHAiOjE1MjQ5MjYxNzV9.xhvdfaWkLVZ_HLwYQuPet_2vlxNF7AoYgX-XRufiOj0'
},
}
) /*end fetch */
.then(results => results.json())
.then(data => this.setState({ data: data }))
}
render() {
console.log(this.state.data);
return (
<div>
<Navbar />
<div className="container">
<div className="clientContainer">
{
this.state.data.map( item =>(
<div>
{item.clientName}
</div>
))
}
</div>
</div>
<Footer />
</div>
);
}
}
export default MemberInfo;
I'm still new when it comes to using ReactJS to fetch API data; could I get some help with this please?