Angular HttpClient未设置授权标头 Angular 4.3 HttpClient不发送授权标头 在get请求中添加授权"标头 我检查了更多的问题,但是没有一个问题能帮助我解决问题.我已经坚持了将近一个星期.也许我无法正确理解某些内容,但是它无法正常工作.如果不清楚,或者我应该添加更多信息,请告诉我.谢谢您的阅读.屏幕托管为文本角头对象: 标头:Map(0){}lazyInit:HttpHeaders {normalizedNames:Map(0),lazyUpdate:null,标头:地图(0)}lazyUpdate:数组(1)0:名称:授权"op:"a"值:基本NTY2MTI0OnVuZGVmaW5lZA =="__proto__:对象长度:1__proto__:数组(0)normalizedNames:Map(0){}__proto__:对象 它应该是什么样的 请求URL:http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName/请求方法:GET状态码:200 OK远程地址:127.0.0.1:8888推荐人政策:降级时不推荐人访问控制允许标题:*访问控制允许方法:*访问控制允许来源:*连接方式:关闭内容长度:20内容类型:text/html;字符集= UTF-8日期:2018年10月18日,星期四10:09:38 GMT语法:dssession = 63572.937476.131783,dssessionexpires = 1197188接受:application/json接受编码:gzip,deflate,br接受语言:en-GB,en; q = 0.9,nl-NL; q = 0.8,nl; q = 0.7,en-US; q = 0.6授权:基本NTY2MTI0Og ==快取控制:max-age = 0内容类型:文本/纯文本;字符集= UTF-8主机:localhost:8080If-Modified-Since:1990年10月1日,星期一,05:00:00 GMT代理连接:保持活动状态引荐来源:http://localhost:8080/用户代理:Mozilla/5.0(Windows NT 10.0; Win64; x64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/69.0.3497.100 Safari/537.36 我的模样是什么 请求URL:http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName请求方法:OPTIONS状态码:401未经授权远端地址:[:: 1]:8080推荐人政策:降级时不推荐人访问控制允许标题:*访问控制允许方法:*访问控制允许来源:*连接方式:关闭内容长度:49内容类型:text/html;字符集= utf-8日期:2018年10月18日星期四13:47:12 GMTWWW-Authenticate:基本领域="REST"接受: */*接受编码:gzip,deflate,br接受语言:en-GB,en; q = 0.9,nl-NL; q = 0.8,nl; q = 0.7,en-US; q = 0.6访问控制请求标头:授权访问控制请求方法:GET连接:保持活动状态主机:localhost:8080来源:http://localhost:8100用户代理:Mozilla/5.0(Windows NT 10.0; Win64; x64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/69.0.3497.100 Safari/537.36 控制台错误: VM1038:1选项http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName401(未经授权)(匿名)@ VM1038:1(索引):1加载失败http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName:的响应预检没有HTTP正常状态. Fiddler Raw:请求 选项http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName HTTP/1.1主机:localhost:8080连接:保持活动状态访问控制请求方法:GET来源:http://localhost:8100用户代理:Mozilla/5.0(Windows NT 10.0; Win64; x64)AppleWebKit/537.36(KHTML,如Gecko)Chrome/69.0.3497.100 Safari/537.36访问控制请求标头:授权,x身份验证,x身份验证模拟接受: */*接受编码:gzip,deflate,br接受语言:en-GB,en; q = 0.9,nl-NL; q = 0.8,nl; q = 0.7,en-US; q = 0.6 响应 HTTP/1.1 401未经授权连接方式:关闭内容类型:text/html;字符集= utf-8内容长度:49日期:2018年10月18日,星期四10:06:24 GMT访问控制允许来源:*访问控制允许标题:*访问控制允许方法:*WWW-Authenticate:基本领域="REST"< HTML>< BODY>< B> 401未经授权</B>/BODY</HTML> 解决方案我通过将以下代码段添加到Delphi中的 TWebModule1.WebModuleBeforeDispatch 中来对其进行了修复: if Trim(Request.GetFieldByName('Access-Control-Request-Headers'))<>'' 然后开始Response.SetCustomHeader('Access-Control-Allow-Headers',Request.GetFieldByName('Access-Control-Request-Headers'));处理:= True;结尾; 来源: Delphi的Datasnap ISAPI模块上的CORS问题 ProblemI am trying to send Request Headers, specifically, an authorization header.The authorization Header should look something like this:Authorization: Basic NTY2MTI0Og== In a list of headers.Where Basic indicates that it is encoded with base64.I'm positive it does get added to the get request made in Angular:Request in Angular.Although I'm not sure what op is.(btw, I'm not yet allowed to embed the image)This is what it should look like: From the standard Datasnap Delphi projectThis is what mine look like: enter image description hereAs you can see, the Authorization header is missing.ErrorsConsole Error: ScreenshotFiddler Raw request: ScreenshotThis is how I create the Rest call:public authRestCall(auth: string): Observable<string> { var headers = new HttpHeaders; //HttpHeaders is immutable headers = headers.append('Authorization', 'Basic ' + auth); return this.http.get<string>(this.localUrl + 'DSAdmin/GetPlatformName', {headers: headers});}Alternativeanother way I tried doing this was to create an object like this, described in the Angular guideconst httpOptions = { headers: new HttpHeaders({ 'Authorization': 'NTY2MTI0OnVuZGVmaW5lZA==' })};which I would then add like this:return this.http.get<string>(this.localUrl + 'DSAdmin/GetPlatformName', httpOptions;DelphiIn the backend server (A Delphi datasnap module) I have configured CORS like this:Response.setCustomHeader('Access-Control-Allow-Origin','*');Response.SetCustomHeader('Access-Control-Allow-Headers','*');Response.SetCustomHeader('Access-Control-Allow-Methods','*');Versions- Angular 5 - ionic-angular: 3.9.2 - List item - Delphi RAD studio 10.1 BerlinSources that did not solve it for meAngular 4.3 HTTPClient Basic Authorization not workingAngular HttpClient not setting Authorization headerAngular 4.3 HttpClient doesn't send Authorization headerAdding "Authorization" header in get requestI have checked many more questions, but none of them helped me solve the problem. I've been stuck on this for almost a week now. Maybe I'm not understanding something correctly, but it is not working.If something is not clear or if I should add more info about something, please let me know.Thank you for reading.Screenshosts as textAngular headers object:headers: Map(0) {}lazyInit: HttpHeaders {normalizedNames: Map(0), lazyUpdate: null, headers:Map(0)}lazyUpdate: Array(1) 0: name: "Authorization" op: "a" value: "Basic NTY2MTI0OnVuZGVmaW5lZA==" __proto__: Object length: 1 __proto__: Array(0)normalizedNames: Map(0) {}__proto__: ObjectWhat it should look like:Request URL: http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName/Request Method: GETStatus Code: 200 OKRemote Address: 127.0.0.1:8888Referrer Policy: no-referrer-when-downgradeAccess-Control-Allow-Headers: *Access-Control-Allow-Methods: *Access-Control-Allow-Origin: *Connection: closeContent-Length: 20Content-Type: text/html; charset=UTF-8Date: Thu, 18 Oct 2018 10:09:38 GMTPragma: dssession=63572.937476.131783,dssessionexpires=1197188Accept: application/jsonAccept-Encoding: gzip, deflate, brAccept-Language: en-GB,en;q=0.9,nl-NL;q=0.8,nl;q=0.7,en-US;q=0.6Authorization: Basic NTY2MTI0Og==Cache-Control: max-age=0Content-Type: text/plain;charset=UTF-8Host: localhost:8080If-Modified-Since: Mon, 1 Oct 1990 05:00:00 GMTProxy-Connection: keep-aliveReferer: http://localhost:8080/User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36What mine looks like:Request URL: http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformNameRequest Method: OPTIONSStatus Code: 401 UnauthorizedRemote Address: [::1]:8080Referrer Policy: no-referrer-when-downgradeAccess-Control-Allow-Headers: *Access-Control-Allow-Methods: *Access-Control-Allow-Origin: *Connection: closeContent-Length: 49Content-Type: text/html; charset=utf-8Date: Thu, 18 Oct 2018 13:47:12 GMTWWW-Authenticate: Basic realm="REST"Accept: */*Accept-Encoding: gzip, deflate, brAccept-Language: en-GB,en;q=0.9,nl-NL;q=0.8,nl;q=0.7,en-US;q=0.6Access-Control-Request-Headers: authorizationAccess-Control-Request-Method: GETConnection: keep-aliveHost: localhost:8080Origin: http://localhost:8100User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36Console error:VM1038:1 OPTIONS http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName401 (Unauthorized)(anonymous) @ VM1038:1(index):1 Failed to loadhttp://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName: Response forpreflight does not have HTTP ok status.Fiddler Raw:RequestOPTIONS http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName HTTP/1.1Host: localhost:8080Connection: keep-aliveAccess-Control-Request-Method: GETOrigin: http://localhost:8100User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36Access-Control-Request-Headers: authorization,x-authentication,x-authentication-impersonateAccept: */*Accept-Encoding: gzip, deflate, brAccept-Language: en-GB,en;q=0.9,nl-NL;q=0.8,nl;q=0.7,en-US;q=0.6ResponseHTTP/1.1 401 UnauthorizedConnection: closeContent-Type: text/html; charset=utf-8Content-Length: 49Date: Thu, 18 Oct 2018 10:06:24 GMTAccess-Control-Allow-Origin: *Access-Control-Allow-Headers: *Access-Control-Allow-Methods: *WWW-Authenticate: Basic realm="REST"<HTML><BODY><B>401 Unauthorized</B></BODY></HTML> 解决方案 I fixed it by adding the following piece of code to the TWebModule1.WebModuleBeforeDispatch in Delphi:if Trim(Request.GetFieldByName('Access-Control-Request-Headers')) <> '' thenbegin Response.SetCustomHeader('Access-Control-Allow-Headers', Request.GetFieldByName('Access-Control-Request-Headers')); Handled := True;end;Source: CORS issue on a Delphi's Datasnap ISAPI Module 这篇关于Angular HttpClient授权标头已创建但消失了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!