首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >生产中的外部API问题- React.js

生产中的外部API问题- React.js
EN

Stack Overflow用户
提问于 2021-01-19 00:36:29
回答 2查看 39关注 0票数 0

我构建了一个从外部API呈现数据的应用程序。在开发模式下,一切正常,但在生产模式下,API不会加载。它不会在控制台中返回任何错误。

代码语言:javascript
复制
axios.get("https://api.github.com/repos/....")
  .then(response => ...)
  .catch(error => console.log(error))

axios.get("https://api.github.com/...")
  .then(response => {
      ....
  })
  .catch(error => console.log(error))

谁能告诉我问题出在哪里?

EN

回答 2

Stack Overflow用户

发布于 2021-01-19 00:53:14

您应该检查控制台中的network选项卡,并查看该请求返回的响应代码。只有当该请求的响应代码是以下网站上列出的客户端错误之一时,才会命中catch块:https://httpstatuses.com/

票数 0
EN

Stack Overflow用户

发布于 2021-01-19 00:59:20

完整的代码在这里

注意在开发模式下。

我的注册表单工作正常,登录工作正常,但在生产中没有这些工作!我用谷歌搜索了一周,但没有找到答案。

代码语言:javascript
复制
import React, {useState, useEffect} from 'react';
import axios from 'axios';
import ShowCase from './utils/table.github.user';
import Validator from "../mm-admin/auth/auth.validator"
const Homepage = () => {

const [organisations, setOrgnisations] = useState([])

const [searchContributor, setSearchContributor] = useState('');
const [searchContributorResult, setSearchContributorResult] = useState([]);

const [isAdded, setAdded] = useState(false);

useEffect(() => {
    let cleanup = false;
    requestApi()
    return () => {
        cleanup = true;
    }

}, [searchContributor])

const requestApi = () =>{
    axios.get("https://api.github.com/repos/git/git/contributors", {
        params : {
            rejectUnauthorized: false,//add when working with https sites
            requestCert: false,//add when working with https sites
            agent: false,//add when working with https sites
        }
    })
    .then(response => {
        const all = response.data;
        const result = all.filter(contributor => {
            return contributor.login.toLowerCase().includes(searchContributor.toLowerCase())
        })
        setSearchContributorResult(result)
    })
    .catch(error => console.log(error))
    axios.get("https://api.github.com/organizations",  {
        params : {
            rejectUnauthorized: false,//add when working with https sites
            requestCert: false,//add when working with https sites
            agent: false,//add when working with https sites
        }
    })
    .then(response => {
        const all = response.data;
        const result = all.filter(contributor => {
            return contributor.login.toLowerCase().includes(searchContributor.toLowerCase())
        })
        setOrgnisations(result)
    })
    .catch(error => console.log(error))
}
const makeSearchContr = event => {
    event.preventDefault()
    setSearchContributor(event.target.value)
}
const addFavorite = (favorite, notes) => event => {
    event.preventDefault();
    if (Validator.isAuthenticated()) {
        const id = Validator.isAuthenticated().user._id;
        const favorites = {
            item : JSON.stringify(favorite),
            note : notes
        }
        axios.put("/user/action/" + id, favorites)
        .then(res => {
            setAdded(true)
            const timer = setTimeout(() => {
                setAdded(false)
            }, 4000)
           return () => clearTimeout(timer)
        })
        .catch(error => console.log(error))
    } else {
        console.log("Need to loged")
    }
}
const contributorGit = () => {
    return searchContributorResult.map((contributor, index) => {
        return <ShowCase key={index} item={contributor} status={isAdded} favorite={addFavorite}/>
    })
}
const organisationsGit = () => {
    return organisations.map((organisation, index) => {
        return <ShowCase key={index} item={organisation} favorite={addFavorite}/>
    })
}
return (
    <article>
        <div className="">
                <div className="container">
                    <form>
                        <div className="">
                        </div>
                        <div className="form-group">
                            <input type="text" className="form-control" placeholder="Search" value={searchContributor} onChange={makeSearchContr}/>
                        </div>
                    </form>
                </div>
        </div>
        <div className="github-user" id="github">
            <div className="container">
                <h2>List contributor :</h2>
                <ul style={{paddingLeft : '0px'}}>
                    {contributorGit()}
                </ul>
                <h2>List organisation :</h2>
                <ul style={{paddingLeft : '0px'}}>
                    {organisationsGit()}
                </ul>
            </div>
        </div>
    </article>
)
}
export default Homepage;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65778560

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档