尝试向Spring Controller发送jQuery Post请求 [英] Attempting jquery post request to spring controller

查看:326
本文介绍了尝试向Spring Controller发送jQuery Post请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用jquery向已设置的spring rest控制器发出发布请求. $.get请求可以很好地解决这个问题,但是post请求在控制台中给了我403错误.有没有更好的方法来解决这个问题,或者使其完全正常工作?

I am trying to make a post request with jquery to a spring rest controller I have setup. This works perfectly fine with the $.get request, however the post request is giving me a 403 error in the console. Is there any better way to handle this, or get it working at all?

$("#testBtn").click(() => {
    $.post("/test").done((data) => {
        console.log(data);
    })
});

我的控制器:

@RestController
public class LiveValidationController {
    @PostMapping("/test")
    public String checkEmail() {
       return "hello";
    }
}

推荐答案

403状态=禁止. 您收到此错误,因为您启用了CSRF保护.

403 status = forbidden. You receive this error because you have CSRF protection enabled.

  • 解决方案1:通过js在您的帖子中发送csrf令牌
  • 解决方案2:在spring安全配置中禁用csrf.

如果您对spring-security模块有依赖性,通常默认情况下会启用csrf保护

The csrf protection is usually enabled by default if you have a dependency to spring-security module

这篇关于尝试向Spring Controller发送jQuery Post请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆