[英]React JS fetch function not sending any HTTP request
這個相同的 React APP 一直工作到昨天,今天突然完全停止工作。 提取根本不起作用,它根本沒有發送 HTTP 請求,正如 Firefox 的“網絡”選項卡所觀察到的那樣。 這有什么問題? 這是昨天完美工作的相同代碼,今天突然停止工作。
如您所見,服務器絕對沒有響應。 為什么沒有針對 POST 請求的響應代碼,如第一行所示?
反應代碼:
import React, { useState } from "react";
import { useHistory } from "react-router-dom";
export default function Login(props) {
const [email_id, setEmailId] = useState({ email_id: "" });
const [password, setPassword] = useState("");
const history = useHistory();
const submit = () => {
let finalOrder = JSON.stringify(email_id);
fetch("http://localhost:8080/Project/customer/login", {
method: "POST",
headers: { "Content-type": "application/json" },
body: finalOrder,
}).then((res) => {
if (res.ok) {
history.push("/AllProducts");
}
});
};
// sessionStorage.setItem("customer_id", JSON.stringify(result));
// alert(sessionStorage.getItem("customer_id"));
// history.push("/AllProducts");
const handleEmailChange = (e) => {
setEmailId({ email_id: e.target.value });
};
const handlePasswordChange = (e) => {
setPassword(e.target.value);
};
return (
<form>
<h3>Sign In</h3>
<div className="form-group d-flex w-25 p-3 position-relative">
<label>Email address : </label>
<input
type="email"
className="form-control"
placeholder="Enter email"
value={email_id.email_id}
onChange={handleEmailChange}
/>
</div>
<div className="form-group d-flex w-25 p-3">
<label>Password : </label>
<input
type="password"
className="form-control"
placeholder="Enter password"
value={password}
onChange={handlePasswordChange}
/>
</div>
<div className="form-group d-flex w-25 p-3">
<div className="custom-control custom-checkbox">
<input
type="checkbox"
className="custom-control-input"
id="customCheck1"
/>
<label className="custom-control-label" htmlFor="customCheck1">
Remember me
</label>
</div>
</div>
<button
onClick={submit}
className="btn btn-primary btn-block d-flex w-25 p-3"
>
Submit
</button>
</form>
);
}
這是什么奇怪的行為? 任何人都可以幫忙嗎? @CrossOrigin("http://localhost:3000/") 添加在 JAVA controller 中。提前致謝。
“控制台中的錯誤:未捕獲(承諾)類型錯誤:嘗試獲取資源時出現網絡錯誤。” 通過向按鈕添加type="button"自動解決。 聽起來很奇怪,但卻是真的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.