Html 如何以angular(类型脚本)从浏览器获取客户端IP地址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44553323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get the client ip address from browser in angular(type script)
提问by Ala'a Mezian
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) 我可以例如为用户显示他正在使用的浏览器 任何帮助将不胜感激
回答by Juan Ignacio Liska
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. For an internal network with DHCP, change the URL by the following:
非常感谢,非常好的解决方案我把它作为我问题的基础,但我没有解决它,因为它给了我互联网服务器的公共 IP。对于使用 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
希望为各位朋友服务
回答by seersol
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");
});
回答by Sangwin Gawande
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
您将拥有 IP 的所有详细信息 this.IppDetails