如何将javascript函数中返回的JSON保存为变量?
在从api.ipify.org检索到JSON格式的客户端IP地址后,我试图将其保存在一个变量中。如果我提醒结果,我可以让IP显示,但由于某种原因无法将其保存在变量中 这项工作:如何将javascript函数中返回的JSON保存为变量?,javascript,json,function,variables,ip-address,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"></scri
<script>
function getIP(json) {
alert(json.ip);
}
</script>
<script src="https://api.ipify.org?format=jsonp&callback=getIP"></script>
函数getIP(json){
警报(json.ip);
}
但这不起作用:
<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>
var clientIP=''
函数getIP(json){
clientIP=json.ip;
返回客户端;
}
警惕(clientIP);
我希望能够将数据存储在一个变量中,这样我就可以将它附加到一个嵌入,并将其添加到其自动webhook帖子中
<!-- 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 -->
var ipaddress variable='SOME_IP_ADDRESS'
变量大小={宽度:400,高度:330};
var flashvars={qualityurl:“avq/300p.xml”,accountHash:“bunchorandomstuff”,eid:2,showMenu:“true”,mrt:120,sis:0,asv:1,mv:0,负载: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);}();
如果我可以将IP地址保存为全局变量,那么我可以将其传递到“flash vars”的“payload”键中。您的
警报
将无法工作,因为您的代码没有同步执行,getIP
在警报
语句之后才会被调用。您需要在getIP
函数中触发依赖于clientIP
的任何功能。以下是一个例子:
函数getIP(json){
var event=new CustomEvent('iploaded',{detail:json.ip});
文件、调度事件(事件);
}
document.addEventListener('iploaded',函数(事件){
var IPADDRESSVARIABLE=event.detail;
变量大小={宽度:400,高度:330};
var flashvars={qualityurl:“avq/300p.xml”,accountHash:“bunchorandomstuff”,eid:2,showMenu:“true”,mrt:120,sis:0,asv:1,mv:0,负载: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);}();
});
//模拟jsonp回调
getIP({ip:'127.0.0.1'})代码>第二个代码示例不起作用,因为该变量只在回调函数中给定了一个值,这意味着javascript解释器开始逐行读取和运行代码时,将立即运行同步运行的警报,但是getIP函数只是在jsonp请求返回响应时才被调用。您的第一个代码示例是正确的方法。正如Rob所说,您希望代码同步运行,但事实并非如此 下面是对代码片段的一个小编辑,基本上我已经将警报包装在一个函数中,然后在getIP函数完成执行后调用该函数
<script>
var clientIP = ''
function getIP(json) {
clientIP = json.ip;
alertClientIp();
}
function alertClientIp () {
alert(clientIP);
}
</script>
var clientIP=''
函数getIP(json){
clientIP=json.ip;
alertClientIp();
}
函数alertClientIp(){
警惕(clientIP);
}
上面代码段的代码设计有点糟糕,如果您只需要使用客户端IP一次,那么不必麻烦将其存储为变量,只需将其传递给执行“automated webhook POST”逻辑的函数即可
函数getIP(json){
clientIP=json.ip;
alertClientIp();
}
//接受客户端ip作为参数
功能webhookLogic(客户端ip){
//使用客户端ip执行您的逻辑,
//为了简单起见,我将坚持你的警惕。
警报(客户端ip);
}
关于您的编辑
看起来您将两组逻辑放在两个单独的脚本元素中,您不能将它们合并为一个吗
<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>
函数getIP(json){
clientIP=json.ip;
alertClientIp();
}
//接受客户端ip作为参数
功能webhookLogic(客户端ip){
//使用客户端ip执行您的逻辑,
//为了简单起见,我将坚持你的警惕。
//触发视频包装器代码,不确定是否
//如果此执行方法将破坏你的应用程序。。。
视频包装器(客户端ip);
}
//您最近编辑的视频代码
函数videoWrapper(客户端ip){
var ipaddress variable=client_ip;
变量大小={宽度:400,高度:330};
var flashvars={qualityurl:“avq/300p.xml”,accountHash:“bunchorandomstuff”,eid:2,showMenu:“true”,mrt:120,sis:0,asv:1,mv:0,负载: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);}();
}
如果这个执行链破坏了你的应用程序,那么我认为你需要回到绘图板上,关于你的问题的组成,你想做什么是很清楚的,但是你的问题缺少一点元数据,关于这个逻辑是如何“组合在一起”的 试试这个: 函数getIP(json){ 返回json.ip; } var json={“ip”:“111.22.33.44”} var clientIP=getIP(json);
警惕(clientIP)代码>我找到了一个解决方法!我将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 -->
//用于存储客户端IP地址的隐藏容器
//函数来检索客户端IP地址
函数getIP(json){
document.getElementById('ipContainer')。innerHTML=json.ip;
}
//
//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 -->