Angular HttpClient authorization header created but disappearing
我正在尝试发送请求标头,特别是授权标头。
授权标头应如下所示:
其中
我很肯定它会添加到Angular中的get请求中:
Angular中的请求。
尽管我不确定
(顺便说一句,我还不允许嵌入图像)
它应该是这样的:从标准的Datasnap Delphi项目中
这是我的样子:在此处输入图片描述
如您所见,缺少Authorization标头。
错误
控制台错误:屏幕截图
Fiddler Raw请求:屏幕截图
这是我创建Rest调用的方式:
1 2 3 4 5 6 | 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}); } |
选择
我尝试执行此操作的另一种方法是创建一个这样的对象,如《 Angular指南》中所述
1 2 3 4 5 | const httpOptions = { headers: new HttpHeaders({ 'Authorization': 'NTY2MTI0OnVuZGVmaW5lZA==' }) }; |
然后我会这样添加:
1 | return this.http.get<string>(this.localUrl + 'DSAdmin/GetPlatformName', httpOptions; |
德尔斐
在后端服务器(一个Delphi datasnap模块)中,我已经这样配置了CORS:
1 2 3 | Response.setCustomHeader('Access-Control-Allow-Origin','*'); Response.SetCustomHeader('Access-Control-Allow-Headers','*'); Response.SetCustomHeader('Access-Control-Allow-Methods','*'); |
版本号
- ionic-angular: 3.9.2
- List item
- Delphi RAD studio 10.1 Berlin
没有为我解决问题的消息来源
Angular 4.3 HTTPClient基本授权不起作用
角HttpClient未设置授权标头
Angular 4.3 HttpClient不发送授权标头
添加"授权"?获取请求中的标头
我检查了更多的问题,但是没有一个问题帮助我解决了这个问题。我已经坚持了将近一个星期。也许我无法正确理解某些内容,但是它无法正常工作。
如果不清楚,或者我应该添加更多信息,请告诉我。
感谢您的阅读。
Screenshosts作为文本
角标题对象:
1 2 3 4 5 6 7 8 9 10 11 12 13 | 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__: Object |
它应该是什么样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | Request URL: http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName/ Request Method: GET Status Code: 200 OK Remote Address: 127.0.0.1:8888 Referrer Policy: no-referrer-when-downgrade Access-Control-Allow-Headers: * Access-Control-Allow-Methods: * Access-Control-Allow-Origin: * Connection: close Content-Length: 20 Content-Type: text/html; charset=UTF-8 Date: Thu, 18 Oct 2018 10:09:38 GMT Pragma: dssession=63572.937476.131783,dssessionexpires=1197188 Accept: application/json Accept-Encoding: gzip, deflate, br Accept-Language: en-GB,en;q=0.9,nl-NL;q=0.8,nl;q=0.7,en-US;q=0.6 Authorization: Basic NTY2MTI0Og== Cache-Control: max-age=0 Content-Type: text/plain;charset=UTF-8 Host: localhost:8080 If-Modified-Since: Mon, 1 Oct 1990 05:00:00 GMT Proxy-Connection: keep-alive Referer: 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.36 |
我的模样是什么:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | Request URL: http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName Request Method: OPTIONS Status Code: 401 Unauthorized Remote Address: [::1]:8080 Referrer Policy: no-referrer-when-downgrade Access-Control-Allow-Headers: * Access-Control-Allow-Methods: * Access-Control-Allow-Origin: * Connection: close Content-Length: 49 Content-Type: text/html; charset=utf-8 Date: Thu, 18 Oct 2018 13:47:12 GMT WWW-Authenticate: Basic realm="REST" Accept: */* Accept-Encoding: gzip, deflate, br Accept-Language: en-GB,en;q=0.9,nl-NL;q=0.8,nl;q=0.7,en-US;q=0.6 Access-Control-Request-Headers: authorization Access-Control-Request-Method: GET Connection: keep-alive Host: localhost:8080 Origin: http://localhost:8100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36 |
控制台错误:
1 2 3 4 5 6 7 | VM1038:1 OPTIONS http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName 401 (Unauthorized) (anonymous) @ VM1038:1 (index):1 Failed to load http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName: Response for preflight does not have HTTP ok status. |
Fiddler Raw:
请求
1 2 3 4 5 6 7 8 9 10 | OPTIONS http://localhost:8080/datasnap/rest/DSAdmin/GetPlatformName HTTP/1.1 Host: localhost:8080 Connection: keep-alive Access-Control-Request-Method: GET Origin: http://localhost:8100 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36 Access-Control-Request-Headers: authorization,x-authentication,x-authentication-impersonate Accept: */* Accept-Encoding: gzip, deflate, br Accept-Language: en-GB,en;q=0.9,nl-NL;q=0.8,nl;q=0.7,en-US;q=0.6 |
响应
1 2 3 4 5 6 7 8 9 10 | HTTP/1.1 401 Unauthorized Connection: close Content-Type: text/html; charset=utf-8 Content-Length: 49 Date: Thu, 18 Oct 2018 10:06:24 GMT Access-Control-Allow-Origin: * Access-Control-Allow-Headers: * Access-Control-Allow-Methods: * WWW-Authenticate: Basic realm="REST" <HTML><BODY>401 Unauthorized</BODY></HTML> |
我通过在Delphi中的
1 2 3 4 5 | if Trim(Request.GetFieldByName('Access-Control-Request-Headers')) <> '' then begin Response.SetCustomHeader('Access-Control-Allow-Headers', Request.GetFieldByName('Access-Control-Request-Headers')); Handled := True; end; |
来源:Delphi的Datasnap ISAPI模块上的CORS问题