[英]Error CORS when i want redirecting localhost:4000 to localhost:3000
[英]Redirecting localhost:3000 to localhost:3000/newpage
我是 React 的新手,想知道如何在單擊時將按鈕路由到新頁面。 這就是我的應用程序現在的樣子。 我正在向服務器發布一個對象,然后我希望轉到一個新頁面。 現在我在 localhost:3000 上運行這個應用程序,我想路由到 localhost:3000/orders。
class App extends Component {
placeOrder = () => {
let {cupcakes, startDate, orders} = this.state;
const order = {
cupcakes: cupcakes,
delivery_date: startDate.toISOString()
}
const newOrder = [...orders, order]
this.setState({orders: newOrder})
fetch('http://localhost:4000/cupcakes/orders', {
method: 'POST',
body: JSON.stringify(orders)
})
.then(function(response) {
if(response.ok) {
return response.json();
}
}).then(function(responseBody){
console.log(responseBody)
})
.catch(function(error) {
console.log("Request failed", error);
});
}
render() {
<button onClick={this.placeOrder}>Place Order</button>
}
}
如果您想從前端導航,則需要使用 React Router 或用於導航的任何工具來實現它。 我不知道你如何設置前端導航來幫助解決這個問題。
另一種方法是在后端實現它,具體取決於您如何構建前端。 類似res.redirect('/orders')
如果你想要最簡單的(但不是最好的,react router 真的很有用)重定向,你可以做window.location.href = http://localhost:3000/orders
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.