【问题标题】:Postman gives response but ReactJS code throws error邮递员给出响应但 ReactJS 代码抛出错误
【发布时间】:2018-03-18 15:27:01
【问题描述】:

我正在尝试从 API 获取记录,但是当我使用以下代码时它返回不成功的响应。但同时,它在邮递员中使用状态码 200 给了我正确的响应。这是我的代码。请帮忙:

import React, {Component} from "react";

    const urlEndPoint = myEndPoint => "https://api.github.com/users/adhishlal"

    export default class Categories extends Component {
     constructor(props) {
       super(props)
       this.state = {
          requestFailed: false
       }
     }

    componentDidMount() {


       fetch(urlEndPoint(this.props.myEndPoint))


         .then(response => {
           if (!response.ok) {
             throw Error(response.statusText)
           }
           return response
         })

         .then(d => response.json())


         .then(response => {
           this.setState({
             responseData: d
           })
         }, () =>
         {
           this.setState({
             requestFailed: true
           })
         })


     }

    render() {

    //If request is failed
    if(this.state.requestFailed)
    {
      return <div>Request Failed</div>
    }

    //Waiting for data to load from Zoho
    if(!this.state.responseData)
    {
      return <div>Loading...</div>
    }

    //Finally populate the data when loaded


    var indents = [];
    for (var i = 0; i < 10; i++) {

     indents.push(

      <div className="col-sm-4 col-xs-12"><div className="cr-openings">
      <p className="no-margin font-color-lightGrey no-of-openings">{i+1}</p><p className="catg-openings font-color-white">{this.state.responseData.name}</p>
      <p className="font-color-lightGrey city-openings no-margin">Bangalore , Chennai , Delhi NCR , Hyderabad , Mumbai</p>
    </div>
    </div>

      );
    }


    return (
      <section className="dotted">
          <div className="dotted-bg-dark">
            <div className="container padding-80-0">
              <div className="row m-b-30">
                {indents}
                </div>
              </div>
          </div>
      </section>
    );






    }
    }

你能建议我在哪里犯错吗?您可以在 restclient 或 postman 中运行 API 以查看响应。

【问题讨论】:

  • 听起来像是 CORS 问题。您的端点网址是否启用了 CORS?
  • @Souradeep Nanda 也检查了 CORS。它没有帮助。

标签: json reactjs api fetch fetch-api


【解决方案1】:

我尚未完成您的代码,但会向您展示如何使用 fetch 获取数据。

     fetch('https://api.github.com/users/adhishlal')
     .then(response => {
       response.json().then(function(data){
         console.log(data);
       })

代码中的data 将是您需要的数据。

fetch() 请求的响应是一个 Stream 对象,这意味着当我们调用 json() 方法时,会返回一个 Promise,因为读取流是异步发生的。

在我看来,改用axios 会让生活变得更轻松。

希望对你有帮助

【讨论】:

    【解决方案2】:

    上面写的代码几乎没有错误。在某些地方返回了不正确的值。同样,setState 是为错误的变量完成的。修复这些将使其运行良好。

    我已经更新了导致问题的一段代码

    .then(d => d.json())  //Return d.json() and not response.json()
       .then(response => {
          this.setState({
            responseData: response, //setState to response and not d
          })
       }, () =>
    

    【讨论】:

      猜你喜欢
      • 2017-06-15
      • 2019-06-22
      • 2016-11-22
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多