原创 JS:检查网络是IPv4还是IPv6
仅供个人测试学习之用,本站不对稳定性负责。
通过一段 js 代码可以显示用户请求网络是 IPv4 访问优先,还是 IPv6 访问优先。
效果
如果你没有 IPv6 的网络环境,可以先用手机流量访问该页面。
代码
<template>
<div>
<a href='https://ipw.cn' target='_blank'> <b>访客IP:</b> {{IP}},您的网络 {{IPVersion}} 访问优先</a>
</div>
</template>
<script>
import Axios from "axios";
export default {
name: "Home",
data(){
return{
IP: "",
IPVersion: "",
}
},
headers:
{
'X-Requested-With': 'XMLHttpRequest',
'Access-Control-Allow-Origin': "*"
},
methods:{
getData(){
var that=this
var api = "https://test.ipw.cn/api/ip/myip?json";
Axios.get(api)
.then(function (res) {
// handle success
console.log(res.data);
console.log(res.data.IP);
console.log(res.data.IPVersion);
that.IP = res.data.IP
that.IPVersion = res.data.IPVersion
})
.catch(function (error) {
// handle error
console.log(error);
})
.then(function () {
// always executed
});
},
},
mounted(){
this.getData();
},
}
</script>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
访客IP: ,您的网络 访问优先
本站运行于腾讯云