如何使用angular.js和drywall(用户管理系统)创建post request,这需要csrf令牌



如何使用需要csrf令牌的angular.js和drywall(用户管理系统)创建post request ?

您这边的一些信息或代码将会有所帮助。但我会试一试:

一个基本的POST请求应该是这样的:

function login() {
  return $http.post('your-domain.com', {someData: "foobar"}).success(function(response) {
    // do something with your data
  }).error(function(error) {
    // do something with the error
  });
}

如果您想为每个请求包含CSRF令牌,您可以从HTML代码中读取它,并使用$http.defaults.headers对象。在app.js中这样做:

var yourApp = angular.module("yourApp", [
]).config(function() {
}).run(function($http) {
  var csfrToken = $("meta[name='csrf-token']").attr("content");
  $http.defaults.headers.post['X-CSRF-TOKEN'] = csfrToken;
});

根据生成CSRF令牌的框架,您必须调整jQuery选择器$("meta[name='csrf-token']").attr("content")

最新更新