Javascript Axios方法获取:我可以';我的表中没有显示数据

Javascript Axios方法获取:我可以';我的表中没有显示数据,javascript,json,reactjs,axios,Javascript,Json,Reactjs,Axios,我想在我的网站上显示一个JSON。我正在使用一个带有安全令牌的API,在我想在我的页面中显示json的数据之前,一切都很好,因为在我的控制台中我可以看到数据。我希望你能帮助我 import React from 'react'; import axios from 'axios'; const Table = () => { const url = 'someApi'; const token = 'someToken' axios(url, { method: 'get

我想在我的网站上显示一个JSON。我正在使用一个带有安全令牌的API,在我想在我的页面中显示json的数据之前,一切都很好,因为在我的控制台中我可以看到数据。我希望你能帮助我

import React from 'react';
import axios from 'axios';

const Table = () => {

const url = 'someApi';
const token = 'someToken'   
axios(url, {
    method: 'get',
    headers: {
        'Authorization': `Bearer ${token}`
    },
    'body': JSON.stringify({
         name: this.state.name,
         quantity: this.state.quantity,
         price: this.state.price,
     })
})
// .then(response => response.json())
.then(response => {
    console.log(response);
})
.catch(error => console.error(error));

    return(
       <>
        <table>
            <thead>
                <tr>
                    <th>sku</th>
                    <th>name</th>
                    <th>quantity</th>
                    <th>price</th>
                </tr>
            </thead>
            <tbody>
            </tbody>
        </table>
       </>
    )
}

export default Table;
从“React”导入React;
从“axios”导入axios;
常数表=()=>{
consturl='someApi';
const token='someToken'
axios(url{
方法:“get”,
标题:{
'Authorization':'Bearer${token}`
},
“body”:JSON.stringify({
名称:this.state.name,
数量:this.state.quantity,
普莱斯:这个州普莱斯,
})
})
//.then(response=>response.json())
。然后(响应=>{
控制台日志(响应);
})
.catch(error=>console.error(error));
返回(
sku
名称
量
价格
)
}
导出默认表;

我认为你应该学习更多关于react以及如何使用map功能的知识,我更喜欢在youtube上看一些关于它的Tut

我将分享一些简单的axios和Mapping数据的用法,希望能对你有所帮助

const Table=()=>{
const[data,setData]=React.useState(null);
React.useffect(()=>{
如果(!数据){
axios(“https://jsonplaceholder.typicode.com/users", {
方法:“获取”,
})
.然后((res)=>setData(res.data))
.catch((错误)=>console.error(错误));
}
},[数据];
返回(
身份证件
名称
电子邮件
电话
{数据
?数据映射((d)=>(
{d.id}
{d.name}
{d.email}
{d.phone}
))
:null}
);
}
ReactDOM.render(
,
document.getElementById(“react”)
);


这是否回答了您的问题?也许有一个更好的副本:将其包装到useEffect hooka中,当响应到达时,使用setState存储响应,从而使用它构建表。您应该限制何时运行
useEffect
。现在,每次组件渲染时都会运行它。这意味着至少两次,一次作为初始渲染,然后在第一次获取数据时再次渲染。因此,您过度使用了API。例如,
useffect(()=>{…},[])
将只获取一次数据。它在数据为空时运行,您是否检查了useffect中的if语句,顺便说一句,问题是关于在表上显示数据!我将在useffect上添加“data”,所以只需渲染一次,并在其为null时获取数据。