如何将javascript函数中返回的JSON保存为变量?

时间:2017-04-08 03:28:46

标签: javascript json function variables ip-address

我试图在从api.ipify.org以JSON格式检索变量后将客户端IP地址保存在变量中。如果我提醒结果但是由于某种原因无法将其保存在变量中,我可以显示IP。

这有效:

<script>

function getIP(json) {
    alert(json.ip);
}

</script>
<script src="https://api.ipify.org?format=jsonp&callback=getIP"></script>

但这不起作用:

<script>

var clientIP = ''

function getIP(json) {
    clientIP = json.ip;
    return clientIP;
}

alert(clientIP);

</script>
<script src="https://api.ipify.org?format=jsonp&callback=getIP"></script>

我希望能够将数据存储在变量中,以便将其附加到嵌入中,然后将其添加到自动webhook POST中。

<!-- begin video recorder code --><script type="text/javascript">
var IPADDRESSVARIABLE = 'SOME_IP_ADDRESS'
var size = {width:400,height:330};
var flashvars = {qualityurl: "avq/300p.xml",accountHash:"BUNCHOFRANDOMSTUFF", eid:2, showMenu:"true", mrt:120,sis:0,asv:1,mv:0, payload: IPADDRESSVARIABLE};
(function() {var pipe = document.createElement('script'); pipe.type = 'text/javascript'; pipe.async = true;pipe.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 's1.addpipe.com/1.3/pipe.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(pipe, s);})();
</script>
<div id="hdfvr-content"> </div>
<!-- end video recorder code -->

如果我可以将IP地址保存为全局变量,那么我可以将其传递给&#39;有效负载&#39; &#39; flash vars&#39;。

的关键

5 个答案:

答案 0 :(得分:0)

您的alert无效,因为您的代码未同步执行,getIP alert语句之后才会被调用。您需要在clientIP函数内触发任何依赖于getIP的功能。这是一个例子:

function getIP(json) {
   var event = new CustomEvent('iploaded', { detail: json.ip });
   document.dispatchEvent(event);
}

document.addEventListener('iploaded', function(event) {
   var IPADDRESSVARIABLE = event.detail;
   var size = {width:400,height:330};
   var flashvars = {qualityurl: "avq/300p.xml",accountHash:"BUNCHOFRANDOMSTUFF", eid:2, showMenu:"true", mrt:120,sis:0,asv:1,mv:0, payload: IPADDRESSVARIABLE};
   (function() {var pipe = document.createElement('script'); pipe.type = 'text/javascript'; pipe.async = true;pipe.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 's1.addpipe.com/1.3/pipe.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(pipe, s);})();
});

// simulate jsonp callback
getIP({ ip: '127.0.0.1' });

此外,return

中无需getIP

答案 1 :(得分:0)

第二个代码示例不起作用,因为变量仅在回调函数内部给出一个值,这意味着一旦javascript解释器开始逐行读取和运行代码,同步运行的警报就会运行,但getIP函数仅在jsonp请求返回响应时稍后调用。你的第一个代码示例是正确的方法。

答案 2 :(得分:0)

正如Rob所说,你期望代码同步运行但事实并非如此。

这是一个可以工作的代码片段的小编辑,基本上我已经将警报包装在一个函数中,然后我在getIP函数执行完毕后调用该函数。

<script>

var clientIP = ''

function getIP(json) {
    clientIP = json.ip;
    alertClientIp();
}

function alertClientIp () {
    alert(clientIP);
}

</script>

上面代码片段的代码设计有点讨厌,如果你只需要使用客户端IP一次,那么就不要把它作为变量存储,只需将它传递给执行你“自动webhook POST”的函数“逻辑。

<script>

    function getIP(json) {
        clientIP = json.ip;
        alertClientIp();
    }

    //Accept the client_ip as a param
    function webhookLogic (client_ip) {

       //Execute your logic with the client_ip, 
       //for simplicity I'll stick to your alert.

       alert(client_ip);
    }

    </script>

关于您的修改

看起来你将两组逻辑放在两个独立的脚本元素中,你能否将它们合并为一个?

<script>

    function getIP(json) {
        clientIP = json.ip;
        alertClientIp();
    }

    //Accept the client_ip as a param
    function webhookLogic (client_ip) {

       //Execute your logic with the client_ip, 
       //for simplicity i'll stick to your alert.

       //Trigger your video wrapper code, unsure if 
       //if this method of execution will break your app...
       videoWrapper(client_ip);
    }

     //Your video code from your latest edit
    function videoWrapper (client_ip) {
        var IPADDRESSVARIABLE = client_ip;
        var size = {width:400,height:330};
        var flashvars = {qualityurl: "avq/300p.xml",accountHash:"BUNCHOFRANDOMSTUFF", eid:2, showMenu:"true", mrt:120,sis:0,asv:1,mv:0, payload: IPADDRESSVARIABLE};
    (function() {var pipe = document.createElement('script'); pipe.type = 'text/javascript'; pipe.async = true;pipe.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 's1.addpipe.com/1.3/pipe.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(pipe, s);})();
   }
    </script>

如果该执行链破坏了您的应用程序,那么我认为您需要回到关于问题构成的绘图板,很清楚您想要做什么,但您的问题缺少一些元数据这个逻辑如何“融合在一起”。

答案 3 :(得分:0)

试试这个:

function getIP(json) {
    return json.ip;
}

var json = { "ip": "111.22.33.44"}
var clientIP = getIP(json);

alert(clientIP);

答案 4 :(得分:0)

我找到了解决方法!我将IP函数的结果存储到隐藏的div中以充当容器。然后我在嵌入代码中声明了一个变量并将其设置为innerHMTL。它可能不是最优雅的,但它完全符合我的要求!

//hidden container to store the client IP address
<div id = 'ipContainer' style='display:none'></div>

//function to retrieve the client IP address
<script>
function getIP(json) {
    document.getElementById('ipContainer').innerHTML = json.ip;
}
</script>

//shortened version of the URL that returns the IP
<script src='http://www.api.ipify.org'><script>


//embed code for the video recorder
<script>
<!-- begin video recorder code --><script type="text/javascript">
var clientIP = document.getElementById('ipContainer').innerHTML;
var size = {width:400,height:330};


//I passed the clientIP variable into the payload element of the flashvars object
var flashvars = {qualityurl: "avq/300p.xml",accountHash:"RANDOM ACCOUNT HASH", eid:2, showMenu:"true", mrt:120,sis:0,asv:1,mv:0, payload:clientIP}; //Here i passed the clientIP which is nor stored as a variable
(function() {var pipe = document.createElement('script'); pipe.type = 'text/javascript'; pipe.async = true;pipe.src = ('https:' == document.location.protocol ? 'https://' : 'http://') + 's1.addpipe.com/1.3/pipe.js';var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(pipe, s);})();
</script>
<div id="hdfvr-content"> </div>
<!-- end video recorder code -->