首页 > 解决方案 > 无法加载资源错误:net::ERR_CONNECTION_REFUSED

问题描述

我在前端使用 react JS,后端使用 .NET API,我正在尝试从 API 获取数据,我启用了 CORS(我在启动文件中添加了一些配置),一切正常。在 react js 中,我想使用我的 API 获取数据,所以这是我显示数据列表的代码:

import React from "react";
import ReactDOM from "react-dom";
import "antd/dist/antd.css";
import { List, Avatar, Button, Spin } from "antd";
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";

import reqwest from "reqwest";

const fakeDataUrl =
  "http://localhost:51492/api/experience/";

class LoadMoreList extends React.Component {
  state = {
    loading: true,
    loadingMore: false,
    showLoadingMore: true,
    data: []
  };

  componentDidMount() {
    this.getData(res => {
      this.setState({
        loading: false,
        data: res.results
      });
    });
  }

  getData = callback => {
    reqwest({
      url: fakeDataUrl,
      type: "json",
      method: "get",
      contentType: "application/json",
      success: res => {
        callback(res);
      }
    });
  };

  onLoadMore = () => {
    this.setState({
      loadingMore: true
    });
    this.getData(res => {
      const data = this.state.data.concat(res.results);
      this.setState(
        {
          data,
          loadingMore: false
        },
        () => {
          // Resetting window's offsetTop so as to display react-virtualized demo underfloor.
          // In real scene, you can using public method of react-virtualized:
          // https://stackoverflow.com/questions/46700726/how-to-use-public-method-updateposition-of-react-virtualized
          window.dispatchEvent(new Event("resize"));
        }
      );
    });
  };

  render() {
    const { loading, loadingMore, showLoadingMore, data } = this.state;
    const loadMore = showLoadingMore ? (
      <div
        style={{
          textAlign: "center",
          marginTop: 12,
          height: 32,
          lineHeight: "32px"
        }}
      >
        {loadingMore && <Spin />}
        {!loadingMore && (
          <Button onClick={this.onLoadMore}>loading more</Button>
        )}
      </div>
    ) : null;
    return (
      <List
        style={{
          width: "50%",
          left: "25%"
        }}
        className="demo-loadmore-list"
        loading={loading}
        itemLayout="horizontal"
        loadMore={loadMore}
        dataSource={data}
        renderItem={item => (
          <List.Item
            actions={[
              <Button type="primary" icon="user-add">
                suivre
              </Button>,
              <a>Message</a>
            ]}
          >
            <List.Item.Meta
              avatar={
                <a>
                  <Avatar src="https://zos.alipayobjects.com/rmsportal/ODTLcjxAfvqbxHnVXCYX.png" />{" "}
                </a>
              }
              title={<a href="https://ant.design">{item.titre}</a>}
             />
          </List.Item>
        )}
      />
    );
  }
}

LoadMoreList.propTypes = {
  classes: PropTypes.object.isRequired
};

export default withStyles()(LoadMoreList);

此代码将显示经验列表,它允许我们单击 LoadMore 加载更多数据,但我在启动项目时得到了什么:

在此处输入图像描述

在 DevTools 中我得到这个错误 Failed to load resource: net::ERR_CONNECTION_REFUSED

我在谷歌上寻找了很多解决方案,但没有人帮助我

谢谢你帮助我。

标签: reactjsapiasp.net-web-apifetchfetch-api

解决方案


推荐阅读