Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

410
Visualizações
Fetch() GET request in reactjs throwing error

 import React, { Component } from 'react'


export default class Games extends Component {
state ={
    loading: true,
    
    
    
}
   async componentDidMount(){
   
    const url="https://api.steampowered.com/ISteamApps/GetAppList/v0001/";
    const response = await fetch(url);
    const data= await response.json();
    this.setState({game: data.applist[0],loading:false});
    
}
render() {
    return (
        <div>
            {this.state.loading||!this.state.game?(<div>loading...</div> ):(
            <div>
                <div>{this.state.game.appid}</div>
                <div>{this.state.game.name}</div>
            </div>)}
        </div>
    );
}
}

Access to fetch at 'https://api.steampowered.com/ISteamApps/GetAppList/v0001/' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled. Games.js:14 GET https://api.steampowered.com/ISteamApps/GetAppList/v0001/ net::ERR_FAILED

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

This isn't a react specific bug. Let me explain what's happening here.

Simplified explanation to CORS: (more here)

CORS is the mechanism which allows the server to send a response back for an api (in your case url=https://api.steampowered.com/ISteamApps/GetAppList/v0001/) only when the request is coming from a specific domain/origin.

For example using CORS we can allow server to respond only to API Requests made from www.steampowered.com origin. (and not localhost and other origins)

Now coming to:

If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled

This means that you can still get the response but of type opaque. Run this and see the response:

async function apiCall() {
    const url = 'https://api.steampowered.com/ISteamApps/GetAppList/v0001/'
    const response = await fetch(url, {
        mode: 'no-cors',
    })
    console.log(response)
}

try {
    apiCall()
} catch (e) {
    console.log('Err is', e.message)
}

From developers.google.com:

An opaque response is for a request made for a resource on a different origin that doesn't return CORS headers. With an opaque response we won't be able to read the data returned or view the status of the request, meaning we can't check if the request was successful or not.

You can use proxies to bypass this. Although if you own the aforementioned url, you can configure to grant yourself the necessary access.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda