Search code examples
javascriptapigithubgithub-pagesfetch-api

why api calls are not working in git hub page


I just finish my news site and while running in localhost of vs code, its working perfectly but on hosting in gh- page api is not working. here is the link of my repository:- https://github.com/Ankitsahu640/Express-Time-News

and here is link of hosted gh-page:- https://ankitsahu640.github.io/Express-Time-News/

that's how my site seen in local host of vs code


Solution

  • I tried to replicate your code and I realized the third party api you are sending request to https://newsapi.org/v2/top-headlines?country=in&apiKey=dbddeef57b58421aafd6a26534fd9d8d

    Does not allow you to send request from broswer except from localhost, thats why it works on your local machine.

    Here the complete response:

    {
        status: "error",
        code: "corsNotAllowed",
        message: "Requests from the browser are not allowed on the Developer plan, except from localhost."
    }