Angular6 向标头添加授权

Angular6 向标头添加授权,angular6,angular-http-interceptors,xtrareport,Angular6,Angular Http Interceptors,Xtrareport,我正在使用Angular 6和DevXpress Xtra报告模块。我想在标题中添加授权。我正在使用HttpInterceptor添加授权,它与我的所有其他模块(我的xtra报表模块除外)都能正常工作,所以我尝试在我的模块内使用jquery传递授权(如下图所示),但这不起作用,我对它不是很满意。 我正在userId中获取令牌 const userId = localStorage.getItem('access_token'); $.ajaxSetup({ cache: true,

我正在使用Angular 6和DevXpress Xtra报告模块。我想在标题中添加授权。我正在使用HttpInterceptor添加授权,它与我的所有其他模块(我的xtra报表模块除外)都能正常工作,所以我尝试在我的模块内使用jquery传递授权(如下图所示),但这不起作用,我对它不是很满意。 我正在userId中获取令牌

const userId = localStorage.getItem('access_token');
$.ajaxSetup({
    cache: true,
    type: 'POST',
    xhrFields: {
        withCredentials: true,
        Authorization: `Bearer ${userId}`,
    },
    complete: function (result) {
        // console.log('complete');
    },
    error: function (jqXHR, status, erreur) {
        // console.log('error');
    },
    beforeSend: function (xhr, settings) {
        console.log(`beforeSend beforeSend beforeSend beforeSend beforeSend beforeSend + ${userId}`);
        xhr.withCredentials = true;
        xhr.Authorization = `Bearer ${userId}`;
    }
});
我不知道为什么HttpInterceptor与我所有的模块一起工作,除了xtra报告模块,可能是因为我收费太低,或者我不知道,如果有其他信息,请告诉我。
谢谢你

我通过添加带有授权的标题解决了这个问题,所以它变成这样:

const userId=localStorage.getItem('access_token');
$.ajaxSetup({
是的,
键入:“POST”,
xhrFields:{
事实上,
},
标题:{
授权:`Bearer${userId}`,
},
完成:功能(结果){
//console.log('complete');
},
错误:函数(jqXHR、状态、erreur){
//console.log('error');
},
发送前:功能(xhr、设置){
log(`beforeSend beforeSend beforeSend beforeSend beforeSend beforeSend+${userId}`);
xhr.withCredentials=true;
xhr.Authorization=`Bearer${userId}`;
}
});

希望有一天这能对某人有所帮助

通常,当您使用ASP.NET中的C#作为后端时,在控制器上使用Authorize属性时,应该注意身份验证/授权及其机制。验证您的Chrome开发者控制台中发生了什么以检测错误,如果错误来自后端或与前端有关(通常devexpress报告具有文档预览控件,可以使用一些变量进行更改)。我通过添加标题解决了此问题:{}并在其中传递授权,而不是在xhrFields中传递授权