【问题标题】:How do I fix this axios GET request? Keep getting 404 status code如何修复此 axios GET 请求?不断收到 404 状态码
【发布时间】:2019-04-30 00:52:58
【问题描述】:

尝试将 json 数据获取到前端并尝试了一堆版本的 axios 请求,但不断收到 404 状态码。

这是前端格式的一个例子:

class App extends Component {
  constructor () {
    super();
    this.state = {
      message: ''
    };

    this.handleClick = this.handleClick.bind(this);
  }

  handleClick () {
    axios.get('./hello')
      .then(response => {
        this.setState({ message: response.data.text });
      })
      .catch(error => {
        console.log(error);
      });
  }

  render () {
    return (
      <div className="button-container">
        <button className='button' onClick={this.handleClick}>Click Me</button>
        <p>{this.state.message}</p>
      </div>
    )
  }
}

和后端路由:

@app.route('/hello')
def hello_world():
   return jsonify(text='hello world')

错误消息显示“加载资源失败:服务器响应状态为 404(未找到)”或显示 http://localhost:5003/hello 不存在

【问题讨论】:

  • 您可以访问您提到的链接吗?本地主机

标签: python reactjs axios


【解决方案1】:
  1. 首先检查你的服务器,哪个url和端口,你的api暴露了
  2. 您需要将完整的 url 传递给 axios 构造函数,否则它将向托管您的客户端的同一源/url 发送请求,例如localhost:3000 的 webapp。

所以你的代码将是

const SERVER_URL = 'http:localhost:5000'
axios.get(`${SERVER_URL}/hello`)

【讨论】:

  • 你的 api url 是什么?
猜你喜欢
  • 2021-04-10
  • 2019-11-05
  • 2020-07-08
  • 2020-04-05
  • 1970-01-01
  • 2021-04-25
  • 2022-11-13
  • 2019-08-15
  • 1970-01-01
相关资源
最近更新 更多