我试图从deerawan修改工作示例,以包含另一个需要标题的网站的标题。我得到了下面的解决方案,它似乎工作得很好。但是当我添加subscribe()时,它仍然工作,但在控制台中得到异常:
我该怎么做才能修复这个警告信息?谢谢。
原始代码:
export class UserService {
private serviceUrl = 'https://jsonplaceholder.typicode.com/users';
constructor(private http: HttpClient) { }
getUser(): Observable<User[]> {
return this.http.get<User[]>(this.serviceUrl);
}
}解决方案代码:
export class NWSForecast {
private config = {
headers: {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'
}
};
private serviceUrl = 'https://api.weather.gov/gridpoints/OKX/36,38/forecast';
constructor(private http: HttpClient) { }
getUser(): Observable<any> {
// first argument is URL, put config as second argument
return this.http.get<any>(this.serviceUrl, this.config);
}
}修改了解决方案,以捕获响应。但它也有例外:
export class AppComponent {
private config = {
headers: {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'
}
};
weathers: any;
private serviceUrl = 'https://api.weather.gov/gridpoints/OKX/36,38/forecast';
constructor(private http: HttpClient) { }
getWeather() {
this.http.get<Weather>(this.serviceUrl, this.config).subscribe(
val => {
this.weathers = val;
console.log('this.weather ====> ', this.weathers);
});
}
}控制台中的错误消息:Refused to set unsafe header "User-Agent" http.js:1436
发布于 2019-02-01 22:59:26
因为需要将URL作为get的第一个参数来提供,所以您得到了错误。
this.http.get<Weather>(url, this.config);发布于 2019-02-01 23:26:40
角的http.get()采用两个参数,网址和选项。您将它们作为1传递,这就导致了这个问题!下面是我经常在第7角中向服务添加自定义头的方式:
import { HttpClient, HttpHeaders } from '@angular/common/http';
export class NWSForecast {
private headerObj = new HttpHeaders({'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'})
private serviceUrl = 'https://api.weather.gov/gridpoints/OKX/36,38/forecast'
constructor(private http: HttpClient) { }
getUser(): Observable<Weather> {
console.log(this.http.get<Weather>(this.config));
return this.http.get<Weather>(this.serviceUrl, {headers: this.headerObj});
}
}您还可以选择传递整个对象,而不是在http.get()中指定headers参数。
const httpOptions = {
headers: new HttpHeaders({
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'
})
};然后你就会回来:
return this.http.get<Weather>(this.serviceUrl, httpOptions);我经常发现角文档有很好的例子-- 角HTTP指南-添加报头
发布于 2019-02-01 23:27:36
基于文档,HttpClient.get的第一个参数是url,第二个参数是配置。
因此,您的代码应该如下所示:
export class NWSForecast {
private config = {
headers: {
'User-Agent' : 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.153 Safari/537.36'
}
};
private serviceUrl = 'https://api.weather.gov/gridpoints/OKX/36,38/forecast';
constructor(private http: HttpClient) { }
getUser(): Observable<any> {
// first argument is URL, put config as second argument
return this.http.get<any>(this.serviceUrl, this.config);
}
}测试过它是否有效

https://stackoverflow.com/questions/54488207
复制相似问题