简体   繁体   English

API 在 localhost 中运行,但在部署时没有

[英]API running in localhost but not when deployed

This was written in React, I am new to using states and react in general and the API I used is working on my local host but not when I deploy it with GitHub.这是用 React 编写的,我不熟悉使用状态和一般反应,我使用的 API 在我的本地主机上工作,但当我使用 GitHub 部署它时却没有。 The GitHub has all the changes and it isn't loading, still. GitHub 进行了所有更改,但仍然没有加载。 Help?帮助?

const joke_api = "http://api.icndb.com/jokes/random?firstName=Aaryaman&lastName=Rattan";

const Dad = () => {
    const [joke, setJoke] = useState('');
    const generate = ()=>{
        fetch(joke_api)
    .then(response => response.json())
    .then(data => setJoke(data.value.joke))
    }
    useEffect(() =>{
        generate();
    }, [])
    
    
    return (
        <section>
         <div className="container portfolio__container">
             <h5 id= 'sec'>Stressed?</h5>
               <h2 id = "sec2">Generate a random joke.</h2>

            <article className= "portfolio__item">
             <div>
             <p >{joke}</p>
             <div className="portfolio__item-cta">
             <button className="btn btn-primary" onClick= {generate}> <h2>More? More.</h2></button>
             {/* <a href= "" className= "btn btn-primary"  onClick= {generate}>More? More.</a> */}
             </div>
            </div> 
            </article>
             </div>
             </section>
    )
}

export default Dad```

Don't know how exactly you deploy it, but I suspect, that that's because a browser's security.不知道您如何部署它,但我怀疑那是因为浏览器的安全性。 You say about GitHub, so if it's hosted there – it should be available on some https-host.你说的是 GitHub,所以如果它托管在那里——它应该在一些 https 主机上可用。 Normally, a browser will not allow you to fetch any non-https hosts from a https-page – and a url in your code has http-protocol, that's why it may be blocked.通常,浏览器不允许您从 https 页面获取任何非 https 主机 - 而您的代码中的 url 具有 http 协议,这就是它可能被阻止的原因。 In such case you'll see a corresponding warning in a browser's console, just check it.在这种情况下,您会在浏览器的控制台中看到相应的警告,只需检查一下即可。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 部署到 Heroku 时如何使 api 端点目标用户的本地主机 - How to make api endpoint target user's localhost when deployed to Heroku Javascript功能可以在localhost上运行,但在部署到Heroku时则不行 - Javascript features work on localhost but not when deployed to Heroku API 从 localhost 部署到 github 抛出 404 错误 - API deployed from localhost to github throw 404 error Angular Meteor应用程序可在localhost中运行,但在部署到服务器时会失败 - Angular Meteor app works in localhost but fails when deployed to server JS 和 Image 目录加载在本地主机中,但在部署 express 时不加载 - JS and Image directories loading in localhost but not when deployed express 我的 env 变量在 localhost 上工作,但在 Netlify 上部署时未定义 - My env variable works on localhost but is undefined when deployed on Netlify Azure 存储 SAS 令牌在 localhost 上工作,但在部署在 Azure Z30136395F018719317C521 上时不起作用 - Azure Storage SAS token working on localhost but not when deployed on Azure Kubernetes Javascript 应用程序在本地主机上运行,但在部署时无法正常运行 - Javscript App is running on local Host but when it is deployed it not running properly 在 localhost 上运行示例时导航栏未扩展 - Navbar not expanding when running example on localhost 在本地主机中运行应用程序时,应用程序缓存不起作用? - App cache is not working when running an application in localhost?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM