【问题标题】:How to get response from any ip address in angular 7如何从角度 7 中的任何 IP 地址获得响应
【发布时间】: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
    })
  }

}

我面临的问题是,当我向youtubegoogle 发送请求时,我得到了响应

但是当我向某些local-ip 发送请求时,我得到了响应

其实我的主要目的是获得本地 ip 的响应

更新

如果我想从我正在开发此应用程序的电脑上获得响应,假设我有 ip:192.168.9.9。向我的电脑发送请求但connection refused,然后 应该配置什么?

请指导!!!

【问题讨论】:

  • 似乎CORS 问题
  • google 不接受来自您 IP 的连接
  • @PardeepJain 是的,但是设置标题,我设置错了吗?
  • 这也应该在后端,否则不起作用
  • CORS 问题应该由服务器端而不是 Angular 来解决

标签: angular request response angular7


【解决方案1】:

我们这里似乎有两个不同的问题:

一方面,您尝试访问由于 CORS 策略 (see here) 而被阻止的 http://www.google.com
访问 http://www.youtube.com

时可能是一样的

另一方面,您尝试访问本地 IP。由于不同的原因,连接可能会被拒绝。您的服务器/计算机无法访问或出现其他问题。尝试先使用 cmd ping 192.168.9.9 ping 您的计算机。如果无法访问,请尝试配置您的防火墙(在两台设备上)。
检查端口是否设置正确,您的服务器是否正在运行并且可以从服务器本身访问。
如果没有额外的信息,很难说更多。

【讨论】:

  • 我可以通过终端ping我的电脑,没问题
  • 使用 API 测试工具测试您的服务器。例如:getpostman.com/apps 如果你通过这个到达你的服务器并得到正确的响应,你可以试试你的客户端
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-09-26
  • 2022-01-21
  • 1970-01-01
  • 2019-05-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多