我正在为应用设置身份验证.在我发布登录请求后,会发送JSON Web令牌作为响应.我可以通过Ajax将它附加到标头.问题是在登录后使用window.location.pathname重定向,因为它不是Ajax请求,它没有将标记附加到标头.我该如何解决这个问题?
$.ajaxSetup({
headers: {
'x-access-token': window.localStorage.jwt
}
});
var Auth = {
signup: function () {
console.log('signuppp');
var userSignup = {
username: $('#usernameSignup').val(),
password: $('#passwordSignup').val()
};
console.log(userSignup)
return $.post('/api/users/register', userSignup, function (resp) {
console.log('resp: ',resp);
window.localStorage.setItem('jwt', resp.token);
//does not have x-access-token header
window.location.pathname = '/';
})
},Run Code Online (Sandbox Code Playgroud)
我正在使用Angular 2 HTTP,并有一个订阅响应的组件.但是,当出现错误时,catch方法不会将错误返回给订阅的组件.它只是把它扔进控制台.
saveFinalize(fcData: LastForecastInterface) {
let responseData = JSON.stringify(fcData);
let body = responseData;
const headers = new Headers();
headers.append('Content-Type', 'application/json;charset=UTF-8');
return this.http.post('/saveFinalize', body, { headers: headers })
.map((data: Response) => data.json())
.catch(this.handleError);
}
public handleError(error: Response | any) {
console.log('err: ', error)
let errMsg: string;
if (error instanceof Response) {
const body = error.json() || '';
const err = body.error || JSON.stringify(body);
errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
} else {
errMsg = error.message ? error.message : …Run Code Online (Sandbox Code Playgroud)