简体   繁体   English

如何以角度(打字稿)从浏览器获取客户端IP地址

[英]How to get the client ip address from browser in angular (typescript)

Hey there i would really appreciate if you can provide me with an example where a type script class can get the client ip address and the browser that the client is using and to set those values in variables嘿,如果您能给我提供一个示例,其中类型脚本类可以获取客户端 IP 地址和客户端正在使用的浏览器并在变量中设置这些值,我将不胜感激

i want to do this in type script not in java script is that possible and if not how to do it with type script我想在类型脚本中而不是在 Java 脚本中执行此操作是可能的,如果不是如何使用类型脚本来执行此操作

- So For Example i can 1) set those variables while submitting the form to the data base in the back end 2)i can for example display for the user the browser he is using any help would be appreciated thanks - 因此,例如,我可以 1) 在将表单提交到后端数据库时设置这些变量 2) 我可以例如为用户显示他正在使用的浏览器 任何帮助将不胜感激

Thank you very much, very good solution I took it as a basis for my problem but I did not solve it because it gave me the public IP of the internet server.非常感谢,非常好的解决方案我把它作为我问题的基础,但我没有解决它,因为它给了我互联网服务器的公共 IP。 For an internal network with DHCP, change the URL by the following:对于使用 DHCP 的内部网络,请通过以下方式更改 URL:

  getIpCliente(): Observable<string> {
      return this.http.get('http://api.ipify.org/?format=jsonp&callback=JSONP_CALLBACK') // ...using post request '
      .map((res:Response) => {console.log('res ', res);
                              console.log('res.json() ', res.text());
                              //console.log('parseado ', JSON.parse(res.text()));
                              console.log('parseado  stringify ', JSON.stringify(res.text()));
                              let ipVar = res.text();
                              let num = ipVar.indexOf(":");
                              let num2 = ipVar.indexOf("\"});");
                              ipVar = ipVar.slice(num+2,num2);
                              console.log('ipVar -- ',ipVar);
                              return ipVar}); // ...and calling .json() on the response to return data
      //.catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...errors if any
  }

I hope to serve you friends希望为各位朋友服务

You should try like this你应该像这样尝试

var json = 'http://ipv4.myexternalip.com/json';
   $http.get(json).then(function(result) {
    console.log(result.data.ip)
},  function(e) {
   alert("error");
});

Try the services of https://geolocation-db.com to get the public ip address of the user.尝试使用https://geolocation-db.com的服务来获取用户的公共 IP 地址。

import { HttpClient } from "@angular/common/http";
import { catchError, tap } from "rxjs/operators";

this.http.get<any>('https://geolocation-db.com/json/')
  .pipe(
    catchError(err => {
      return throwError(err);
    }),
    tap(response => {
      console.log(response.IPv4);
    })
  )

Try This :试试这个:

Create Provider and add function with required dependencies :创建 Provider 并添加具有所需依赖项的函数:

import { Injectable }     from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/toPromise';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import 'rxjs/Rx';


 //  Function :

getIP(): Observable<Data[]> {
    return this.http.get('http://ipinfo.io') // ...using post request
    .map((res:Response) => res.json()) // ...and calling .json() on the response to return data
    .catch((error:any) => Observable.throw(error.json().error || 'Server error')); //...errors if any
}

Controller Code :控制器代码:

getIP() {
    this.loading = true;
    this._myIPService.getIP()
    .subscribe(
        IPDetails => this.IppDetails,
        error =>  this.errorMessage = <any>error
        );
}

You will have all the details of IP in this.IppDetails您将在this.IppDetails获得 IP 的所有详细信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM