如何使用_csrf在angularjs中发送POST请求?
我想发送POST请求到spring security下的服务。 如果我使用表单提交(带有隐藏输入),它工作得很好。 但我不知道如何使用$http.post发送请求。 我的表格: 它总是写着“不” 我认为,这需要以webForm的形式发送变量,但如何在$http.post中发送呢 Fiddler编写:如何使用_csrf在angularjs中发送POST请求?,angularjs,jsp,spring-security,csrf,Angularjs,Jsp,Spring Security,Csrf,我想发送POST请求到spring security下的服务。 如果我使用表单提交(带有隐藏输入),它工作得很好。 但我不知道如何使用$http.post发送请求。 我的表格: 它总是写着“不” 我认为,这需要以webForm的形式发送变量,但如何在$http.post中发送呢 Fiddler编写:内容类型为“application/json”;此检查器仅支持“x-www-form-urlencoded” 服务器控制台: org.springframework.web.servlet.PageN
内容类型为“application/json”;此检查器仅支持“x-www-form-urlencoded”
服务器控制台:
org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
WARNING: Request method 'POST' not supported
请帮帮我 我解决了这个问题)
您可能需要在请求头中设置“_csrf”。您正在将其作为方法的参数传递。这行不通。我不太了解angular.js,但您必须将其作为请求头或请求负载传递。我找到了一些参考资料。下面是:
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';
希望这能帮助你解决你的问题。干杯 也许这有帮助:[不,不要帮忙,谢谢
$http.post('http://localhost:8888/rest/post1', {"_csrf": b}, {
"headers" :
{ "_csrf" : b }
}).success(function(data) {
console.log("ok");
}).error(function(){
console.log("no");
});
org.springframework.web.servlet.PageNotFound handleHttpRequestMethodNotSupported
WARNING: Request method 'POST' not supported
var request = $http({
method: "post",
url: "http://localhost:8888/rest/post1",
headers: {'Content-Type': 'application/x-www-form-urlencoded'},
transformRequest: function(){return a+"="+b;},
data: {}
});
$httpProvider.defaults.xsrfCookieName = 'csrftoken';
$httpProvider.defaults.xsrfHeaderName = 'X-CSRFToken';