Jquery POST giving 403 forbidden error in spring mvc

后端 未结 5 1854
余生分开走
余生分开走 2020-12-14 01:41

I want to make a ajax call using $.POST. But I am getting 403 error. But GET works perfectly fine. My code is:

var url = \"/xyz/abc/subscribe?name=\"+name;
$         


        
5条回答
  •  甜味超标
    2020-12-14 02:09

    You might want to add the csrf token to the request.

    Obtaining the token using JSTL should be pretty straightforward. If you are using Thymeleaf, here is how to obtain it.

    
    

    Then, add it to your request:

    var requestData = {
        'paramA': paramA,
        'paramB': paramB,
    };
    requestData[_csrf_param_name] = _csrf_token; // Adds the token
    
    $.ajax({
        type: 'POST',
        url: '...your url...',
        data: requestData,
        ...
    });
    

    If everything goes well, the request should include something like _csrf:1556bced-b323-4a23-ba1d-5d15428d29fa (the csrf token) and you will get a 200 instead of a 403.

提交回复
热议问题