想使用angularjs裡的htpp向後台發送請求,後台是基於tornado的,現在有個用戶唯一識別的token想要放到headers裡面去,也就是{headres:{'token':1}},但是嘗試了很多方法不行,想問下該怎麼做,後台需不需要設定一些東西,多謝
天蓬老师2017-05-15 16:59:52
config中置入以下攔截器
$httpProvider.interceptors.push(['$rootScope', '$q', '$localStorage', function ($rootScope, $q, $localStorage) {
return {
request: function (config) {
// Header - Token
config.headers = config.headers || {};
if ($localStorage.token) {
config.headers.token = $localStorage.token;
};
return config;
},
response: function (response) {
if (response.status == 200) {
// console.log('do something...');
}
return response || $q.when(response);
},
responseError: function (response) {
return $q.reject(response);
}
}
}])
天蓬老师2017-05-15 16:59:52
$.ajax({
type: "GET",
url: "xxxx",
beforeSend: function(request) {
request.setRequestHeader("Token", "1");
},
success: function(result) {
alert(result);
}
});
不過幹嘛用header呢,cookie,請求參數都行啊