首页 > 解决方案 > 如何使用 POST 方法修复 net::err_aborted 415?

问题描述

我正在尝试向我的后端发送 POST 请求,但我收到“net::ERR_ABORTED 415”错误。我在后端使用 Spring,在前端使用 React。我已经禁用了 CORS,因为我遇到了问题。这是反应代码:

const sendInfos = (e) => {
    e.preventDefault()
    fetch('http://localhost:9090/Register', {
        mode: 'no-cors',
        method: 'POST',
        headers: {
            Accept: 'application/json',
            'Content-Type': 'application/json'
        },
        body: JSON.stringify({
            name: name,
            password: password,
            email: email
        })
    });
}

这是Java Spring代码:

@RestController
public class RegistrationController {
    @Autowired
    private UserDao userDao;

    @Autowired
    BCryptPasswordEncoder encoder;

    @PostMapping(value = "/Register")
    @ResponseStatus(code = HttpStatus.CREATED)
    public void PlayerRegistration(@RequestBody UserCredentials userDto) throws UserAlreadyRegisteredException{
        if(userDao.findByEmail(userDto.getEmail()) != null){
            throw new UserAlreadyRegisteredException("This email is already registered on the system.");
        }
        User user = new User();
        user.setName(userDto.getName());
        user.setPassword(encoder.encode(userDto.getPassword()));
        user.setEmail(userDto.getEmail());
        user.setElo(1200);
        userDao.save(user);
    }
}

我已经和我最好的朋友“google”一起寻找答案,但我找到的所有答案都不起作用。有谁知道如何修理它?

标签: javascriptreactjsspring

解决方案


推荐阅读