简体   繁体   English

React JS fetch function 不发送任何 HTTP 请求

[英]React JS fetch function not sending any HTTP request

This very same React APP was working till yesterday and suddenly today it stopped working completely.这个相同的 React APP 一直工作到昨天,今天突然完全停止工作。 The fetch is not working at all, it not sending the HTTP request at all as observed the Network tab of Firefox.提取根本不起作用,它根本没有发送 HTTP 请求,正如 Firefox 的“网络”选项卡所观察到的那样。 What is wrong with this?这有什么问题? It was the same code that perfectly worked yesterday and suddenly today it stops to work.这是昨天完美工作的相同代码,今天突然停止工作。

图片1

图片2

服务器响应到底在哪里

As you can see that there is absolutely no response from the server.如您所见,服务器绝对没有响应。 Why there is no response code against the POST request, as seen in the first line?为什么没有针对 POST 请求的响应代码,如第一行所示?

React Code:反应代码:

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>
  );
}

What is this weird behavior?这是什么奇怪的行为? Can anyone help?任何人都可以帮忙吗? @CrossOrigin("http://localhost:3000/") added in the JAVA controller.Thanks in advance. @CrossOrigin("http://localhost:3000/") 添加在 JAVA controller 中。提前致谢。

PS: Getting perfect response in ARC. PS:在 ARC 中获得完美响应。 在此处输入图像描述

Now it is giving the following error:现在它给出以下错误: 在此处输入图像描述

"Error in the console: Uncaught (in promise) TypeError: NetworkError when attempting to fetch resource." “控制台中的错误:未捕获(承诺)类型错误:尝试获取资源时出现网络错误。” got resolved automatically by adding type="button" to the button.通过向按钮添加type="button"自动解决。 Sounds weird but true.听起来很奇怪,但却是真的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM