Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在react中进行api调用和加载数据_Javascript_Reactjs - Fatal编程技术网

Javascript 如何在react中进行api调用和加载数据

Javascript 如何在react中进行api调用和加载数据,javascript,reactjs,Javascript,Reactjs,在我试图做的是我有一个表,当我点击复选框并点击提交按钮,然后在api中获取数据。我的api正在运行,并在控制台中显示数据。但现在我没有获取数据 我尝试在单击按钮时调用api,但当我选择并选中行,然后单击“提交”按钮时,我无法在控制台中获取数据 当我选择行并单击按钮时,我不会在控制台中获取数据 校验码 谁能帮我解决这个问题。非常感谢 <tbody> <tr> <td> <input t

在我试图做的是我有一个表,当我点击复选框并点击提交按钮,然后在api中获取数据。我的api正在运行,并在控制台中显示数据。但现在我没有获取数据

我尝试在单击按钮时调用api,但当我选择并选中行,然后单击“提交”按钮时,我无法在控制台中获取数据

当我选择行并单击按钮时,我不会在控制台中获取数据

校验码

谁能帮我解决这个问题。非常感谢

<tbody>
          <tr>
            <td>
              <input type="radio" preventDefault name="select" />
            </td>

            <td>dfgrty</td>
            <td>fgfg</td>
            <td>fgfg</td>
            <td>erer</td>
            <td>uuio</td>
          </tr>
        </tbody>
      </table>
    </div>
    <div className="btn-submit">
      <button
        className="btn btn-primary"
        style={{ marginRight: "30px" }}
        type="submit"
      >
        submit
      </button>
    </div>
      

   

dfgrty
fgfg
fgfg
埃勒
乌尤
提交

处理复选框和单选状态,并通过在复选框中添加条件来检查api调用,请尝试下面的代码:我在您的沙盒代码中尝试了它

import React from "react";
import ReactDOM from "react-dom";

class Test extends React.Component {
  constructor(props) {
    super();
    this.state = {
      data: [],
      checkbox: false
    };
  }
  handleClick = () => {
    const apiUrl = "https://mocki.io/v1/b512f8b8-64ab-46e4-9e0c-9db538a0ad9e";
    if (this.state.checkbox) {
      fetch(apiUrl)
        .then((response) => response.json())
        .then((data) => {
          this.setState({ data: data });
          console.log("This is your data", data);
        });
    } else {
      alert("check the radio!");
    }
  };

  render() {
    return (
      <div>
        <div
          className="table-employee"
          style={{ marginTop: "20px", border: " 1.5px solid darkgray" }}
        >
          <table className="table table-hover table-bordered table-sm">
            <thead>
              <tr>
                <th scope="col">Select</th>
                <th scope="col"> LOAD DATE</th>
                <th scope="col"> FILE DATE</th>
                <th scope="col"> SERVICE</th>
                <th scope="col"> PROVISIONER CODE </th>
                <th scope="col"> DESCRIPTION</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>
                  <input
                    value={this.state.checbox}
                    onChange={(e) =>
                      this.setState({ checkbox: !this.state.checkbox })
                    }
                    type="radio"
                    preventDefault
                    name="select"
                  />
                </td>
                {this.state.data.map((row) => {
                  return <td>{row.id}</td>;
                })}
              </tr>
            </tbody>
          </table>
        </div>
        <div className="btn-submit">
          <button
            className="btn btn-primary"
            style={{ marginRight: "30px" }}
            type="submit"
            onClick={this.handleClick}
          >
            submit
          </button>
        </div>
      </div>
    );
  }
}

ReactDOM.render(<Test />, document.getElementById("root"));


从“React”导入React;
从“react dom”导入react dom;
类测试扩展了React.Component{
建造师(道具){
超级();
此.state={
数据:[],
复选框:false
};
}
handleClick=()=>{
常量apiUrl=”https://mocki.io/v1/b512f8b8-64ab-46e4-9e0c-9db538a0ad9e";
if(this.state.checkbox){
获取(APIRL)
.then((response)=>response.json())
。然后((数据)=>{
this.setState({data:data});
log(“这是您的数据”,data);
});
}否则{
警报(“检查收音机!”);
}
};
render(){
返回(
挑选
装货日期
存档日期
服务
供应器代码
描述
this.setState({checkbox:!this.state.checkbox})
}
type=“无线电”
防止违约
name=“选择”
/>
{this.state.data.map((行)=>{
返回{row.id};
})}
提交
);
}
}
render(,document.getElementById(“根”));

处理复选框和单选状态,并通过在复选框中添加条件来检查api调用,请尝试下面的代码:我在您的沙盒代码中尝试了它

import React from "react";
import ReactDOM from "react-dom";

class Test extends React.Component {
  constructor(props) {
    super();
    this.state = {
      data: [],
      checkbox: false
    };
  }
  handleClick = () => {
    const apiUrl = "https://mocki.io/v1/b512f8b8-64ab-46e4-9e0c-9db538a0ad9e";
    if (this.state.checkbox) {
      fetch(apiUrl)
        .then((response) => response.json())
        .then((data) => {
          this.setState({ data: data });
          console.log("This is your data", data);
        });
    } else {
      alert("check the radio!");
    }
  };

  render() {
    return (
      <div>
        <div
          className="table-employee"
          style={{ marginTop: "20px", border: " 1.5px solid darkgray" }}
        >
          <table className="table table-hover table-bordered table-sm">
            <thead>
              <tr>
                <th scope="col">Select</th>
                <th scope="col"> LOAD DATE</th>
                <th scope="col"> FILE DATE</th>
                <th scope="col"> SERVICE</th>
                <th scope="col"> PROVISIONER CODE </th>
                <th scope="col"> DESCRIPTION</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>
                  <input
                    value={this.state.checbox}
                    onChange={(e) =>
                      this.setState({ checkbox: !this.state.checkbox })
                    }
                    type="radio"
                    preventDefault
                    name="select"
                  />
                </td>
                {this.state.data.map((row) => {
                  return <td>{row.id}</td>;
                })}
              </tr>
            </tbody>
          </table>
        </div>
        <div className="btn-submit">
          <button
            className="btn btn-primary"
            style={{ marginRight: "30px" }}
            type="submit"
            onClick={this.handleClick}
          >
            submit
          </button>
        </div>
      </div>
    );
  }
}

ReactDOM.render(<Test />, document.getElementById("root"));


从“React”导入React;
从“react dom”导入react dom;
类测试扩展了React.Component{
建造师(道具){
超级();
此.state={
数据:[],
复选框:false
};
}
handleClick=()=>{
常量apiUrl=”https://mocki.io/v1/b512f8b8-64ab-46e4-9e0c-9db538a0ad9e";
if(this.state.checkbox){
获取(APIRL)
.then((response)=>response.json())
。然后((数据)=>{
this.setState({data:data});
log(“这是您的数据”,data);
});
}否则{
警报(“检查收音机!”);
}
};
render(){
返回(
挑选
装货日期
存档日期
服务
供应器代码
描述
this.setState({checkbox:!this.state.checkbox})
}
type=“无线电”
防止违约
name=“选择”
/>
{this.state.data.map((行)=>{
返回{row.id};
})}
提交
);
}
}
render(,document.getElementById(“根”));

any body help me and any If Suggestion我想在button clickTry上调用api或者进行HTTP api调用。any body help me and any If Suggestion我想在button clickTry上调用api或者进行HTTP api调用。谢谢@faramarz,但是我的表体数据在哪里?我刚刚在一个表行中呈现了数据,```{this.state.data.map((行)=>{return{row.id};})}````它在主体部分。你可以自定义uii我想要表格主体数据这个dfgrty FGFGFG fgfg erer UUIO你能帮我怎么做吗我在哪里可以自定义ui我试着这么做,但我遇到了错误你能给我看一些关于这个@faramarz的@faramarz razmithanks的东西吗这是我的表体数据??我刚刚在一个表行中呈现了数据,``{this.state.data.map((row)=>{return{row.id};})}````它在主体部分。你可以自定义uii想要表格主体数据这个dfgrty fgfg fgfg erer UUIO你能帮我怎么做吗我在哪里可以自定义ui我试着这么做,但我遇到了错误你能给我看些东西@faramarz razmi吗