我正在尝试在React中对我本地IIS上托管的Web API进行API调用。 API端点工作正常。但是调用返回一个空数组。这就是我到目前为止所做的:
export default class MyRequests extends React.Component {
constructor() {
super();
this.state = { isLoaded: false, Data: [], error: null }
}
componentDidMount() {
fetch("http://localhost:8080/api/Request/GetRequest")
.then(res => res.json())
.then(
(result) => {
this.setState({
isLoaded: true,
Data: result.Data
});
},
(error) => {
this.setState({
isLoaded: true,
error
});
}
)
}
render() {
console.log(this.state.Data);
const { data } = this.state;
return (
<div>hi
<br />
</div>
);
}
}
这是来自API端点的我的JSON:
{
"CallbackFunction": null,
"ContentEncoding": null,
"ContentType": null,
"Data": [
{
"ID": 0,
"SiteTitle": "title",
"SiteDescription": "test",
"DateSubmitted": "0001-01-01T00:00:00"
}
],
"JsonRequestBehavior": 1,
"MaxJsonLength": null,
"RecursionLimit": null
}
我在Chrome上启用了CORS扩展。请让我知道你的想法