I am writing a function, which will connect to a server and retrieve a JSON object. It will process it and return a normal array with values to be loaded in some HTML tag. I know it can be done using other ways, but I am using the following code.
我正在编写一个函数,它将连接到服务器并检索JSON对象。它将处理它并返回一个常规数组,其中的值将被加载到一些HTML标记中。我知道可以用其他方法完成,但是我正在使用以下代码。
var names = new Array();
function fetch (key) {
var url = 'server/server.php?key=' + key
$.getJSON(url , sett = function(data) {
$.each(data, function(key, value){
names[key] = value['name'];
});
console.log(names);//works and print the values
});
console.log(names);//doesn't work, shows and empty array
}
I want to return an array which will hold the values filtered by jQuery each, so this function can work as a standard function for any big application. How it can be done?
我想返回一个数组,它将保存jQuery筛选的每个值,因此这个函数可以作为任何大型应用程序的标准函数。如何才能做到呢?
3 个解决方案
#1
1
Like mentioned in comments, your ajax request will take some time (short amount of time, but still time), so your console.log
located outside of the ajax call will execute before the ajax call is complete, showing the original, empty array.
正如注释中提到的,您的ajax请求将花费一些时间(短时间,但仍然是时间),因此您的控制台。位于ajax调用外部的日志将在ajax调用完成之前执行,显示原始的空数组。
You could add a callback parameter to your fetch
function...
您可以向fetch函数添加一个回调参数……
so...
所以…
function fetch (key, callback) {
var url = 'server/server.php?key=' + key
$.getJSON(url , sett = function(data) {
$.each(data, function(key, value){
names[key] = value['name'];
});
// call callback inside the getJSON callback
callback && callback.call(this, names);
});
}
Now, your callback function will have your names
array as argument one.
现在,你的回调函数将你的名字数组作为参数1。
fetch(some_key, function(names){
console.log(names); //should work!
});
#2
0
I would start by trying to add your array elements to the array.
我首先尝试将数组元素添加到数组中。
names.push(value['name']);
If you want more of dictionary approach (which it seems like you do) try this.
如果你想要更多的字典方法(看起来你是这样做的),试试这个。
JavaScript dictionary with names
JavaScript字典名称
Also, after reading the other comments and thinking more... you may want to move your code that loops over the results into the 'success' callback.
同时,在阅读了其他的评论和思考之后……您可能希望将循环遍历结果的代码移到“success”回调中。
http://api.jquery.com/jQuery.getJSON/
http://api.jquery.com/jQuery.getJSON/
Good luck!
好运!
#3
0
I haven't tested this. But the following should work:
我还没有测试。但以下方法应该有效:
var names = new Array();
function fetch (key) {
var url = 'server/server.php?key=' + key
$.ajax({
url: url,
type: "get",
success: function(data) {
$.each(data, function(key, value) {
names[key] = value['name'];
});
console.log(names); // this should still work
},
dataType: "json",
async: false
});
console.log(names); // this should now work
}
However, you really should try to stay away from synchronous requests.
但是,您确实应该尽量远离同步请求。
#1
1
Like mentioned in comments, your ajax request will take some time (short amount of time, but still time), so your console.log
located outside of the ajax call will execute before the ajax call is complete, showing the original, empty array.
正如注释中提到的,您的ajax请求将花费一些时间(短时间,但仍然是时间),因此您的控制台。位于ajax调用外部的日志将在ajax调用完成之前执行,显示原始的空数组。
You could add a callback parameter to your fetch
function...
您可以向fetch函数添加一个回调参数……
so...
所以…
function fetch (key, callback) {
var url = 'server/server.php?key=' + key
$.getJSON(url , sett = function(data) {
$.each(data, function(key, value){
names[key] = value['name'];
});
// call callback inside the getJSON callback
callback && callback.call(this, names);
});
}
Now, your callback function will have your names
array as argument one.
现在,你的回调函数将你的名字数组作为参数1。
fetch(some_key, function(names){
console.log(names); //should work!
});
#2
0
I would start by trying to add your array elements to the array.
我首先尝试将数组元素添加到数组中。
names.push(value['name']);
If you want more of dictionary approach (which it seems like you do) try this.
如果你想要更多的字典方法(看起来你是这样做的),试试这个。
JavaScript dictionary with names
JavaScript字典名称
Also, after reading the other comments and thinking more... you may want to move your code that loops over the results into the 'success' callback.
同时,在阅读了其他的评论和思考之后……您可能希望将循环遍历结果的代码移到“success”回调中。
http://api.jquery.com/jQuery.getJSON/
http://api.jquery.com/jQuery.getJSON/
Good luck!
好运!
#3
0
I haven't tested this. But the following should work:
我还没有测试。但以下方法应该有效:
var names = new Array();
function fetch (key) {
var url = 'server/server.php?key=' + key
$.ajax({
url: url,
type: "get",
success: function(data) {
$.each(data, function(key, value) {
names[key] = value['name'];
});
console.log(names); // this should still work
},
dataType: "json",
async: false
});
console.log(names); // this should now work
}
However, you really should try to stay away from synchronous requests.
但是,您确实应该尽量远离同步请求。