【发布时间】:2019-05-18 16:35:06
【问题描述】:
我想通过向不同的 IP 地址发送请求来获得任何类型的响应
export class DeviceComponent implements OnInit {
response: any
constructor(private deviceService: DeviceService) { }
ngOnInit() {
this.deviceService.ping().subscribe(result => {
console.log(result)
}, err => {
console.log(err)
})
}
}
export class DeviceService {
constructor(private http: HttpClient) { }
ping() {
const httpHeaders = new HttpHeaders({
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
})
let me: string = `http://192.168.10.7:100`
let aw: string = `http://192.168.10.6:100`
let google: string = `https://www.google.com`
let youtube: string = `https://www.youtube.com`
return this.http.get(google, {
headers: httpHeaders
})
}
}
我面临的问题是,当我向youtube 或google 发送请求时,我得到了响应
但是当我向某些local-ip 发送请求时,我得到了响应
其实我的主要目的是获得本地 ip 的响应
更新
如果我想从我正在开发此应用程序的电脑上获得响应,假设我有 ip:192.168.9.9。向我的电脑发送请求但connection refused,然后
应该配置什么?
请指导!!!
【问题讨论】:
-
似乎
CORS问题 -
google 不接受来自您 IP 的连接
-
@PardeepJain 是的,但是设置标题,我设置错了吗?
-
这也应该在后端,否则不起作用
-
CORS 问题应该由服务器端而不是 Angular 来解决
标签: angular request response angular7