Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/git/25.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
Reactjs 通过外部文件进行API调用并返回结果_Reactjs_Api - Fatal编程技术网

Reactjs 通过外部文件进行API调用并返回结果

Reactjs 通过外部文件进行API调用并返回结果,reactjs,api,Reactjs,Api,我试图通过一个单独的文件进行API调用,并返回结果和状态,例如,在检索和显示数据之前显示加载文本 在一个文件中,它运行良好,但现在我移动到像这样的单独文件,它不工作 用户数据表 import React, { Component } from 'react'; import Table from '@material-ui/core/Table'; import TableBody from '@material-ui/core/TableBody'; import TableCell from

我试图通过一个单独的文件进行API调用,并返回结果和状态,例如,在检索和显示数据之前显示加载文本

在一个文件中,它运行良好,但现在我移动到像这样的单独文件,它不工作

用户数据表

import React, { Component } from 'react';
import Table from '@material-ui/core/Table';
import TableBody from '@material-ui/core/TableBody';
import TableCell from '@material-ui/core/TableCell';
import TableHead from '@material-ui/core/TableHead';
import TableRow from '@material-ui/core/TableRow';
import Paper from '@material-ui/core/Paper';
import { withStyles } from '@material-ui/core/styles';
import { getUserData } from './APIcalls/UserSearch.js';

// Styles
const styles = theme => ({
    Table: {
        margin: '10px'
    },
});

class UserDataTable extends Component {
    componentDidMount() {
        var config = { "Access-Control-Allow-Origin": "*" }
        getUserData(config, () => {
            // Success
            console.log();
        },(err) => {
            // Error
            alert(err);
        });
    }

    render() {
        const { classes } = this.props; 

        return (
            <div>
                <Paper className={classes.Table}>
                    <Table>
                        <TableHead className={classes.TableHeader}>
                            <TableRow>
                                <TableCell>Firm</TableCell>
                                <TableCell>Office</TableCell>
                                <TableCell>Sales Code</TableCell>
                                <TableCell>Account</TableCell>
                            </TableRow>
                        </TableHead>
                        <TableBody>
                            <TableRow>
                                <TableCell>1</TableCell>
                                <TableCell>2</TableCell>
                                <TableCell>3</TableCell>
                                <TableCell>4</TableCell>
                            </TableRow>
                            <TableRow>
                                <TableCell>1</TableCell>
                                <TableCell>2</TableCell>
                                <TableCell>3</TableCell>
                                <TableCell>4</TableCell>
                            </TableRow>
                        </TableBody>
                    </Table>
                </Paper>
            </div>
        )
    }
}

export default withStyles(styles)(UserDataTable);

这将解决您的问题

        // Rest of the class code
        getUserData(config, (response) => {
            // Success
            this.setState({
              isLoaded: true,
              items: response,
            });
        },(err) => {
            // Error
            alert(err);
        });
        // Rest of the class code

这将解决您的问题

        // Rest of the class code
        getUserData(config, (response) => {
            // Success
            this.setState({
              isLoaded: true,
              items: response,
            });
        },(err) => {
            // Error
            alert(err);
        });
        // Rest of the class code

调用函数时,发送三个参数config、successCallback和errorCallback,但在函数中接受两个参数。职能应为:

export function getUserData(config, successCallback, errorCallback){

您不应该在API函数中调用
setState
,successCallback应该设置状态。Api函数应该是独立的

当您调用函数时,您发送三个参数config、successCallback和errorCallback,但在函数中您接受两个参数。职能应为:

export function getUserData(config, successCallback, errorCallback){

您不应该在API函数中调用
setState
,successCallback应该设置状态。Api函数应该是独立的

你们回答得这么快,系统在问题5分钟前不允许我接受,哈哈哈,这很有趣你们回答得这么快,系统在问题5分钟前不允许我接受,哈哈哈,这很有趣