Javascript 将返回的数组从(Ajax输出)传递到任何地方用作全局变量
我试图从Ajax输出返回,数组中的两个变量,用于其他位置的另一个单独函数: 但是不知道如何将它传递给新函数 我在这里的尝试:Javascript 将返回的数组从(Ajax输出)传递到任何地方用作全局变量,javascript,jquery,ajax,callback,jquery-callback,Javascript,Jquery,Ajax,Callback,Jquery Callback,我试图从Ajax输出返回,数组中的两个变量,用于其他位置的另一个单独函数: 但是不知道如何将它传递给新函数 我在这里的尝试: var ip,country; function getVisitorData(callback) { $.ajax({ type: "GET", url: "http://freegeoip.net/json/", dataType: "json", async: true, su
var ip,country;
function getVisitorData(callback) {
$.ajax({
type: "GET",
url: "http://freegeoip.net/json/",
dataType: "json",
async: true,
success: function (response) {
ip = response.ip;
country = response.country_code;
callback(response);
},
error: function (response) {
alert("IP thing didn't work.");
}
});
}
function callback(response) {
return [ip, country];
//alert(ip + country); /* <- it works here */
}
getVisitorData(callback);
jQuery(document).ready(function () {
var data = getVisitorData(callback);
var ip = data[0];
var country = data[1];
alert(ip + country);
});
对不起,如果我不清楚的话
非常感谢
function getVisitorData(callback) {
$.ajax({
type: "GET",
url: "http://freegeoip.net/json/",
dataType: "json",
async: true,
success: function (response) {
ip = response.ip;
country = response.country_code;
callback(response);
},
error: function (response) {
alert("IP thing didn't work.");
}
});
}
function callback(data) {
var ip = data[0];
var country = data[1];
return [ip, country];
}
jQuery(document).ready(function () {
getVisitorData(callback);
});
在
回调
函数中执行数据操作,而不是在任何其他函数中执行。有关更多信息,请访问创建全局对象
Var objMyData={}
在该对象中存储ip和国家代码
objMyData.IP=response.IP;
objMyData.Country=response.Country_代码
现在将objMyData对象作为参数传递给任何函数。我不知道为什么需要在回调中返回数组。由于ajax是异步的,它不会等到响应到来时才执行其余语句。所以返回值将给出未定义的值。只有在响应出现后,才能使用/分配响应
function getVisitorData(callback) {
return $.ajax({
type: "GET",
url: "http://freegeoip.net/json/",
dataType: "json",
async: true
});
}
jQuery(document).ready(function () {
$.when(getVisitorData())
.done(function(respone) {
var ip = response.ip;
var country = response.country_code;
//here you can trigger a custom event with the values, wherever you need, just bind the event and use the data.
$(document).trigger("responseDataPublished", [ip, country]);
})
.fail(function(response){
alert("IP thing didn't work.");
});
});
我倾向于在这里作出承诺:
var ip = {};
var country = {};
var getstuff = {};
function callback(response) {
ip = response.ip;
country = response.country_code;
// not really needed return [ip, country];
}
function getVisitorData() {
getstuff = $.ajax({
type: "GET",
url: "http://freegeoip.net/json/",
dataType: "json",
async: true
}).done(callback)
.fail(function (response) {
alert("IP thing didn't work.");
});
}
jQuery(document).ready(function () {
getVisitorData();
getstuff.done(function () {
alert(ip + country);
});
});
这是我们很多人都不知道的一个常见问题,在服务调用成功或失败之前,服务调用旁边的行将执行,因此您需要设置一些超时 //创建一个全局对象。 var objMyData={}
全局对象
函数getVisitorData(){函数调用
$.ajax({`ajax
键入:“获取”,
url:“”,
数据类型:“json”,
async:true,
成功:功能(响应){
//在该对象中存储ip和国家代码。
objMyData.ip=response.ip;
objMyData.Country=response.Country_代码;
},
错误:函数(响应){
警报(“IP问题不起作用”);
}
});
}
jQuery(文档).ready(函数(){
//现在将objMyData对象作为参数传递给任何函数。
getVisitorData();方法调用
setTimeout(函数(){console.log(objMyData);},500);
}); 关于你的更新,你在这里走错了方向,对不起!异步JavaScript根本就不是这样工作的。倾听那些告诉您需要使用返回的数据调用函数而不是将其存储在全局变量中的人,并听取他们的建议。这是你成功解决问题的唯一途径。你可以使用回调,也可以使用承诺——这只是做同一件事的两种方法。你不能做的只是将数据存储在一个全局变量中,并期望它在发出ajax请求后可用。我更新了你的一个fiddle,将其转化为一个工作示例:你不能做的是“返回”数据或将其留在一个全局变量中或诸如此类。问题是:需要数据的代码如何知道何时实际从服务器接收到数据?知道数据准备好的唯一方法是调用回调的时间。谢谢@vinayakj,但我仍然不知道在那之后如何使用输出。我想单独使用
ip
和country
作为全局变量,就像这样:jQuery(document).ready(function(){alert(ip)})代码>所以如果我对此不够清楚,我已经更新了问题!谢谢@Saravana!这是我的尝试(),请检查一下并告诉我错误是什么?。请原谅我是新手!非常感谢。这是我们很多人不知道的常见问题,在服务调用成功或失败之前,服务调用旁边的行将执行,因此需要设置一些超时//创建一个全局对象。var objMyData={};jQuery(document).ready(函数(){//现在将objMyData对象作为参数传递给任何函数。getVisitorData(objMyData);setTimeout(函数(){console.log(objMyData);},500);});谢谢@Mark Schultheiss!对不起,我的问题不清楚!我已经更新了。只想将输出用作全局变量!那就应该工作了var ip={};var country={}代码>执行此操作时请注意:jQuery(document).ready(函数(){var data=getVisitorData(回调);var ip=data[0];var country=data[1];alert(ip+country);})代码>定义新的variablesThanks@jrath!对不起,我的问题不清楚!我已经更新了。只想将输出用作全局变量!
var ip = {};
var country = {};
var getstuff = {};
function callback(response) {
ip = response.ip;
country = response.country_code;
// not really needed return [ip, country];
}
function getVisitorData() {
getstuff = $.ajax({
type: "GET",
url: "http://freegeoip.net/json/",
dataType: "json",
async: true
}).done(callback)
.fail(function (response) {
alert("IP thing didn't work.");
});
}
jQuery(document).ready(function () {
getVisitorData();
getstuff.done(function () {
alert(ip + country);
});
});