Jquery POST giving 403 forbidden error in spring mvc

后端 未结 5 1845
余生分开走
余生分开走 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:28

    If you look to CSRFilter source code, you will see that the filter is waiting for csrfToken on header or query parameter. In my configuration, the key "_csrf" was the right key in query parameter. So, I added this parameter in my post call.

          var csrfCookie = getCsrfCookie();
        		alert(csrfCookie);
        		
        	function getCsrfCookie()
        	{
        		var ret = "";
        		var tab = document.cookie.split(";");
        		
        		if(tab.length>0){
        			var tab1 = tab[0].split("=");
        			if(tab1.length>1)
        			{
        				ret =tab1[1];
        			}
        		}
        		return ret;
        	}
        	
        	$http.post('/testPost?_csrf='+csrfCookie).success(function (response) {
                 alert("post : "+response);
                  return response;
              });

    With this, it works for me.

提交回复
热议问题