Harsh Mittal
Harsh Mittal

Reputation: 57

Unable to Fetch Coinmarketcap API data

This error:

Access to fetch at 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest' from origin 'http://localhost:19006' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource

...is returned by this fetch request.

fetch(
      "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest",
      {
        headers: {
          "Access-Control-Allow-Origin": "*",
          "Access-Control-Allow-Methods": "*",
          "Access-Control-Allow-Headers":
            "'Access-Control-Allow-Headers: Origin, Content-Type, X-Auth-Token'",
          "Content-Type": "application/json",
          "X-CMC_PRO_API_KEY": API_KEY,
        },
      }
    )
      .then((response) => response.json())
      .then((json) => console.log(json))
      .catch((error) => console.error(error));

Why is this happening?

Upvotes: 1

Views: 1920

Answers (1)

Jawwad Ahmed
Jawwad Ahmed

Reputation: 317

1- send request to your backend processing file system (php,cfm,aspx) etc
2- use curl to process your api and send it via your backend code, do not use javascript to fetch "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest" instead use "http://yourdomain.com/yourendpoint"
3- now at your end point use your backend tech to hit "https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest" via curl

Upvotes: 0

Related Questions