I want to use http in angularjs to send a request to the backend. The backend is based on tornado. Now there is a token uniquely recognized by the user that I want to put in the headers, which is {headres:{'token':1}}, But I have tried many methods but it doesn’t work. I would like to ask what should be done. Do I need to set up something in the background? Thank you
我想大声告诉你2017-05-15 16:59:52
Routing configuration$httpProvider.defaults.headers.post['token'] = '123';
天蓬老师2017-05-15 16:59:52
Place the following interceptors in 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);
}
});
But why use headers, cookies, and request parameters?