[英]issue in getting device ip address from a javascript webpage
I have this JavaScript
code in my webpage: 我的网页中有以下JavaScript
代码:
const configDefault = {
Bot: {
// initial sessionAttributes
sessionAttributes: {},
},
};
Now What I want is to set the sessionAttributes
with the ip address
of the device on which this webpage is opened by the user. 现在,我想用用户在其上打开此网页的设备的ip address
设置sessionAttributes
。 Based on this SO post I wrote this code to get the ip address
of the device but I am having issue in first extracting the ip address
out of the returned json
object and then integrating this piece so that sessionAttributes
in my code above is set with the ip address
: 基于此SO帖子,我编写了此代码以获取设备的ip address
,但是我首先sessionAttributes
返回的json
对象中提取ip address
,然后将其集成,以便将上面代码中的sessionAttributes
设置为ip address
:
function myIP() {
$.getJSON('//freegeoip.net/json/?callback=?', function(data) {
JSON.stringify(data, null, 2));
});
}
The json
object in above code return following sample structure. 上面代码中的json
对象返回以下示例结构。 I want to get ip
field out of it and set it to sessionAttributes
in my code at the top: 我想从中获取ip
字段并将其设置为顶部代码中的sessionAttributes
:
{
"ip": "116.12.250.1",
"country_code": "SG",
"country_name": "Singapore",
"region_code": "01",
"region_name": "Central Singapore Community Development Council",
"city": "Singapore",
"zip_code": "",
"time_zone": "Asia/Singapore",
"latitude": 1.2931,
"longitude": 103.8558,
"metro_code": 0
}
I am new to JavaScript so I am unable to find a way to do this. 我是JavaScript的新手,所以找不到解决方法。 Can anyone help me in structuring this right way? 谁能帮助我以正确的方式进行结构设计?
Update: My current code is at http://js.do/code/158408 . 更新:我当前的代码在http://js.do/code/158408 。 I am getting error on $
sign when I run it 我在运行$
符号时遇到错误
const configDefault = {
Bot: {
// initial sessionAttributes
sessionAttributes: {},
},
};
function myIP() {
$.getJSON('//freegeoip.net/json/?callback=?', function(data) {
return JSON.parse(data);
});
}
const ipInformation = myIP();
configDefault.Bot.sessionAttributes.ip = ipipInformation.ip;
You should use JSON.parse instead of stringify. 您应该使用JSON.parse而不是stringify。
const ipInformation = JSON.parse(data);
console.log(ipInformation.ip);
const configDefault = {
Bot: {
// initial sessionAttributes
sessionAttributes: {},
},
};
configDefault.Bot.sessionAttributes.ip = ipipInformation.ip;
console.log(configDefault);
or if you wanted all of ipInformation in sessionAttributes. 或者如果您想在sessionAttributes中使用所有ipInformation。
configDefault.Bot.sessionAttributes = ipipInformation;
As $.getJSON is async, you'll need to do all the work inside the callback: 由于$ .getJSON是异步的,因此您需要在回调中完成所有工作:
let configDefault = {
Bot: {
// initial sessionAttributes
sessionAttributes: {}
}
};
function myIP(configDefault) {
$.getJSON("//freegeoip.net/json/?callback=?", function(data) {
const ipInformation = JSON.parse(data);
configDefault.Bot.sessionAttributes.ip = ipInformation.ip;
console.log(configDefault);
return configDefault;
});
}
configDefault = myIP(configDefault );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.