1

我正在研究web ui基于这个项目的项目。现在我要做的是,当用户打开网页时,ip address应该返回打开此页面的设备的 。为此,我在此处将以下代码添加到 javascript` 代码中:

    function myIP() {
      $.getJSON("//freegeoip.net/json/?callback=?", function(data) {
        const ipInformation = JSON.parse(data);
        console.log(ipInformation.ip);
        return ipInformation.ip;
      });
    }

const sessionAttr = myIP();

我还在同一个代码文件<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>的顶部添加了。javascript现在在该代码文件sessionAttr的第 60 行调用常量(请注意,我已更改为)。sessionAttributes: config.lex.sessionAttributessessionAttributes: config.sessionAttr

当我尝试加载此页面时,没有任何显示。如果我不进行上述更改,则页面会正确加载。所以不知何故,我在我的补充中犯了一些错误,这导致了这个页面。

注意:我完全不熟悉,JavaScript但基于快速搜索,我进行了上述更改。我知道问题出在我正在进行的异步调用中,我浏览了这个建议的链接,但我无法找出正确的结构。谁能为我提供正确的语法,以便页面正确加载并返回ip address客户端并将其设置为sessionAttribute

更新:经过一些建议后,我对代码进行了以下更改(链接在这里 - https://jsfiddle.net/ywdeu0o4/3/

const configDefault = {
  Bot: {
    // initial sessionAttributes
    sessionAttributes: {},
  },
};

$(document).ready(function(){
configDefault.Bot.sessionAttributes.ip = myIP();
 // undefined
});

function myIP() {
    $.getJSON("//freegeoip.net/json/?callback=?", function(data) {
        //console.log(data.ip);
        return data.ip;
    });
}
console.log("myVar:",configDefault.Bot.sessionAttributes.ip);

当我在打开控制台后运行此代码时,我undefined得到configDefault.Bot.sessionAttributes.ip. 为什么它是未定义的而不是 IP 地址?

4

2 回答 2

1

Open the browser dev tool (F12). Very likely you will see an error in the console. For instance the data is already an object, no need to JSON.parse it.

Then there's the asynchronous nature of ajax as Mikael already pointed out. Use a callback function to do something with the data.

EDIT: there was already a link to a page explaining how to use a callback, but maybe it was too much to figure out. You should learn about variables, functions, variable scope, callbacks and async behavior.

Basically you could replace that return with code that updates your global object, and do stuff... or create a new function that you can call back:

const configDefault = {
  Bot: {
    // initial sessionAttributes
    sessionAttributes: {},
  },
};
$(document).ready(function() {
  myIP();
});

function myIP() {
  $.getJSON("//freegeoip.net/json/?callback=?", function(data) {
    // after succesful ajax response call myCallback with data.ip
    myCallback(data.ip); 
  });
}

function myCallback(theIP) {
// do your stuff here
   console.log("Data.ip:", theIP);
   configDefault.Bot.sessionAttributes.ip = theIP;
   console.log("ConfigDefault:", configDefault);
};

Example: https://jsfiddle.net/bvevqdb1/

You can also call the callback without passing parameters and without (). That would pass the whole ajax result to the callback, and you can then process it there: https://jsfiddle.net/bvevqdb1/1/

于 2017-06-23T23:12:59.067 回答
-1

这对我来说似乎很好用: https ://jsfiddle.net/ywdeu0o4/1/

$(document).ready(function(){
  myIP();
});

function myIP() {
    $.getJSON("//freegeoip.net/json/?callback=?", function(data) {
        console.log(data.ip);
        return data.ip;
    });
}
于 2017-06-23T23:01:20.823 回答