简体   繁体   中英

Axios post request not sending data or recieving response

This is my first time trying to do basic authentication and nothing seems to be working. Axios post is not sending data/controllers not returning a response. I cant find exactly where I am going wrong, but I am wondering if the body of my http post request has to match requestbody or what I need to change to get userrepositroy to save form dat in database?

const Login = () => {

    const [registration, setRegistration] = useState("true");

    const [email, setEmail] = useState("");
    const [regPwd, setRegPwd] = useState("");
    const [regUsername, setRegUsername] = useState("");

    const [logUsername, setLogUsername] = useState("");
    const [logPwd, setLogPwd] = useState("");

    const redirect = useNavigate();

    const handleRegSubmit = async (e) => {
        e.preventDefault();
        const registerDTO = {
            email: email,
            username: regUsername,
            password: regPwd
        }
        try {
            const response = await axios.post('http://localhost:8080/registration',
                 {registerDTO} )
                if (response.status === 200) {
                    redirect("/user/${username}")
                } else {
                    setEmail("")
                    setRegUsername("")
                    setRegPwd("")
                }
        }
        catch (error) {
            console.log(error)
        }
    }

    const handleLogSubmit = async (e) => {
        e.preventDefault();
        const logUser = {
            username: logUsername,
            password: logPwd
        }
        try {
            const response = await axios.post('http://localhost:8080/login',
                { logUser })
                if (response.status === 200) {
                    redirect("/user/${username}")
                } else {
                    setLogUsername("")
                    setLogPwd("")
                }
        }
        catch (error) {
            console.log(error)
        };   
    }




Spring boot



@CrossOrigin(origins= "http://localhost:3000")
@RestController
@RequestMapping("")
public class AuthenticationController {

    @Autowired
    UserRepository userRepository;

       
    @PostMapping("/register")
    public ResponseEntity<Object> processRegistration(@RequestBody @Valid RegisterDTO registerDTO,
                                                      BindingResult bindingResult, HttpServletRequest request) {

        if (bindingResult.hasErrors()) {
            return ResponseEntity.badRequest().body("An error occurred!");
        }

        User existingUser = userRepository.findByUsername(registerDTO.getUsername());

        if (existingUser != null) {
            return ResponseEntity.badRequest().body("Username already exists!");
        }


        User user = new User(registerDTO.getEmail(), registerDTO.getUsername(), registerDTO.getPassword());
        userRepository.save(user);

        return ResponseEntity.ok().body(user);
    }

    @PostMapping("/login")
    public ResponseEntity<Object> processLogin(@RequestBody @Valid LoginDTO loginDTO,
                                               BindingResult bindingResult,HttpServletRequest request) {

        if (bindingResult.hasErrors()) {
            return ResponseEntity.badRequest().body("An error occurred!");
        }

        User user = userRepository.findByUsername(loginDTO.getUsername());

        if (user == null) {
            return ResponseEntity.badRequest().body("Username does not exist!");
        }

        String password = loginDTO.getPassword();

        if (!user.isMatchingPassword(password)) {
            return ResponseEntity.badRequest().body("Invalid password!");
        }

        return ResponseEntity.ok(user);
    }

}

您正在发送到http://localhost:8080/registration但将路由定义为localhost:8080/register

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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