从API错误中提取数据,JSON输入意外结束

时间:2018-08-13 16:44:16

标签: json reactjs api fetch

创建API后,我按照此TUTO启用了CORS:https://www.codeproject.com/Articles/1150023/Enable-Cross-origin-Resource-Sharing-CORS-in-ASP-N

我想使用REACT JS从我的API中获取数据,我想显示“经验”列表,所以我使用以下URL:'http://localhost:51492/api/experience/'

这是我的代码,用于从API提取数据:

import React, { Component } from 'react';
import PropTypes from "prop-types";
import { withStyles } from "@material-ui/core/styles";

const API = 'http://localhost:51492/api/experience/';
const DEFAULT_QUERY = 'redux';

class LoadMoreList extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      hits: [],
    };
  }

  componentDidMount() {
    fetch(API + DEFAULT_QUERY)
      .then(response => response.json())
      .then(data => this.setState({ hits: data.hits }));
  }

  render() {
    const { hits } = this.state;

    return (
      <ul>
        {hits.map(hit =>
          <li key={hit.id}>
            <a href={hit.url}>{hit.titre}</a>
          </li>
        )}
      </ul>
    );
  }
}



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

export default withStyles()(LoadMoreList);

但是我得到了:未处理的拒绝(SyntaxError):JSON输入意外结束 在devTools中,我得到了:未捕获(承诺)SyntaxError:JSON输入意外结束

我还配置了文件aplicationhost.config,并添加了它:

<httpProtocol>
 <customHeaders>
  <clear />
   <add name="X-Powered-By" value="ASP.NET" />
   <add name="Access-Control-Allow-Origin" value="*" />
   <add name="Access-Control-Allow-Headers" value="Content-Type" />
   <add name="Access-Control-Allow-Credentials" value="true" />

 </customHeaders>
    <redirectHeaders>
                <clear />
            </redirectHeaders>
</httpProtocol>

我的配置遗漏了什么吗?

感谢您的帮助。


更新: JSON结果是: enter image description here

0 个答案:

没有答案