1
I’m new to Reactjs and I’m learning about API. I’m trying to use a Star Wars API, but it’s not paying off. I’ve used others and they worked.
This is my App.js file:
import React, { Component } from 'react';
import api from './api';
class App extends Component {
state = {
filmes: [],
}
async componentDidMount() {
const response = await api.get('films');
this.setState({ filmes: response.data });
console.log(this.state.filmes);
}
render() {
const { filmes } = this.state;
return (
<div>
<h1>Listar os Filmes</h1>
{filmes.map((filme) => (
<div class="card">
<div class="card-body">
<h2 class="card-title">{filme.results.title}</h2>
<h3 class="card-episode">{filme.results.director}</h3>
<h4 class="card-date">{filme.results.release_date}</h4>
</div>
</div>
))}
</div>
);
};
};
export default App;
And this is my api.js file
import axios from 'axios';
const api = axios.create({
baseURL: 'https://swapi.dev/api/'
});
export default api;
This is the api link: https://swapi.dev/api/films/
The mistake I get is:
Access to Xmlhttprequest at 'https://swapi.dev/api/films' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested Resource.
Failed to load Resource: net::ERR_FAILED
Uncaught (in Promise) Error: Network Error creatat eError (createError.js:16) At Xmlhttprequest.handleError (xhr.js:83)
Unchecked Runtime.lastError: The message port closed before a Response was revived.
Devtools failed to load Sourcemap: Could not load content for Chrome-Extension://gighmmpiobklfepjocnamgkkbiglidom/include.preload.js.map: HTTP error: status code 404, net::ERR_UNKNOWN_URL_SCHEME
Devtools failed to load Sourcemap: Could not load content for Chrome-Extension://gighmmpiobklfepjocnamgkkbiglidom/include.postload.js.map: HTTP error: status code 404, net:ERR_UNKNOWN_URL_SCHEME
In Firefox:
I don’t know what to do and how to fix it.
The server that is searching the data does not support CORS and Axios restricts transactions outside the same origin to occur without CORS headers. To get this data in your application will have rewrite using fetch API in place of Axios using the mode
no-cors
– Augusto Vasques