當我創建和使用新控制器時,看起來無法接近控制器。
這是源代碼,我創建了“BooksController”。控制臺錯誤是“未捕獲(承諾中)SyntaxError: Unexpected token < in JSON at position 0”
export class BooksIndex extends Component {
constructor(props) {
super(props);
this.state = {
books: [],
loading: true
}
}
// page initialized
componentDidMount() {
this.populateBooksData();
}
static renderBooksTable(books) {
return (
<table className='table table-striped' aria-labelledby="tabelLabel">
<thead>
<tr>
<th>Id</th>
<th>Title</th>
<th>Description</th>
<th>Created</th>
</tr>
</thead>
<tbody>
{books.map(book =>
<tr key={book.id}>
<td>{book.id}</td>
<td>{book.title}</td>
<td>{book.description}</td>
<td>{book.created}</td>
</tr>
)}
</tbody>
</table>
);
}
render() {
let content = this.state.loading
? <p><em>Loading...</em></p>
: BooksIndex.renderBooksTable(this.state.books);
return (
<div>
<h1>My Books</h1>
<h2>This is my book</h2>
{content}
</div>
);
}
async populateBooksData() {
const response = await fetch("books");
const data = await response.json();
this.setState({ books: data, loading: false });
}
}
cf) 這是專案執行時的螢屏截圖。Webcaster 是否為創建專案時默認創建的控制器。
在此處輸入影像描述
uj5u.com熱心網友回復:
如果您通過 使用 react spa 模板dotnet new react,那么可能是因為 ClientApp/src 中 setupProxy.js 中的背景關系,默認情況下看起來像這樣:
const context = [
"/weatherforecast",
];
module.exports = function(app) {
const appProxy = createProxyMiddleware(context, {
target: target,
secure: false,
headers: {
Connection: 'Keep-Alive'
}
});
app.use(appProxy);
};
您可以添加/books到該背景關系陣列,或者像我建議的那樣,將其更改為/api然后在您的控制器上使用此屬性(或創建其他人繼承的基本控制器):[Route("api/[controller]")]
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/483900.html
下一篇:如何使用剃須刀頁面取消系結屬性
