这个问题通常出现在使用Angular HttpClient进行HTTP请求时,服务器没有返回请求头信息。因此,我们需要在客户端代码中添加处理这种情况的逻辑。以下是一些可行的
const headers = new HttpHeaders({ 'Content-Type': 'application/json' }); const options = { headers: headers }; this.http.post(url, data, options).subscribe( response => { const headerValue = response.headers.get('Header-Value') ? response.headers.get('Header-Value') : ''; // ... } );
const headers = new HttpHeaders({ 'Content-Type': 'application/json' }); const options = { headers: headers }; this.http.post(url, data, options).subscribe( response => { if (response.headers !== null) { const headerValue = response.headers.get('Header-Value'); // ... } } );
const options = { observe: 'response', responseType: 'text' }; this.http.get(url, options).subscribe( response => { if (response.headers.get('Content-Type') === 'application/json') { const data = JSON.parse(response.body); // ... } } );
通过以上解决方法,我们可以在Angular中处理掉headers.get(...)为null的问题。