全局范围、返回值和 ajax 的问题?
标题是对我的脚本出了什么问题的猜测:
这是在我的 global.js
脚本中:
alert(search.getLabelsNames(); //alerts as undefined.
$('#search').autocomplete({
source: function( request ) {
search.getLabelsNames();
},
minLength:1
});
这是在我的 functions.js
脚本中:
var search;
window.search = {
getLabelsNames:function( search ) {
$.ajax({
url : '../db_scripts/get_labels_names.php',
type: "POST",
data: { id: search }, //this defaults to nothing. not a problem
success : function( data ) {
var dataObj = jQuery.parseJSON( data );
$.each(dataObj, function() {
alert(this.name);
return this.name;
})
}
});
}
}
在每个函数 this.name
从数据库中正确返回每个标签名称。但是当我从 globals.js
调用它时,它返回为 undefined
。如果我返回数字 1,search.getLabelsNames()
会警告 1..,因此查找全局函数没有问题。
这个脚本有什么问题,为什么 global.js
找不到返回的 this.name
?
The title is a guess as to what is going wrong with my script:
This is in my global.js
script:
alert(search.getLabelsNames(); //alerts as undefined.
$('#search').autocomplete({
source: function( request ) {
search.getLabelsNames();
},
minLength:1
});
This is in my functions.js
script:
var search;
window.search = {
getLabelsNames:function( search ) {
$.ajax({
url : '../db_scripts/get_labels_names.php',
type: "POST",
data: { id: search }, //this defaults to nothing. not a problem
success : function( data ) {
var dataObj = jQuery.parseJSON( data );
$.each(dataObj, function() {
alert(this.name);
return this.name;
})
}
});
}
}
In the each function, this.name
, returns every one of the label names correctly from the database. But when I call it from globals.js
, it returns as undefined
. If I return the number 1, the search.getLabelsNames()
alerts 1.. so it has no problem finding the global function.
what is wrong with this script and why can't global.js
find the this.name
that is being returned?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您有两个问题:
您无法从
jQuery.each
回调返回数据。返回值表示是否停止迭代。来自 文档:<块引用>
我们可以通过使回调函数返回 false 来在特定迭代中中断
$.each()
循环。返回非 false 与 for 循环中的 continue 语句相同;它将立即跳到下一次迭代。您无法从 Ajax 回调返回数据。 Ajax 是异步,这意味着在您的情况下,
getLabelsNames
在检索和处理响应之前返回。幸运的是,自动完成插件接受函数作为源。您已经在使用它,但没有正确使用。让它接受第二个参数,这是一个回调。来自文档:
因此,您需要做的就是将此回调传递给
getLabelsNames
,以便可以在 Ajax 调用的success
方法中调用它:请注意,我使用
jQuery.map
[文档] 这里,返回的地方回调中的值有不同的含义。不要将它与jQuery.each
混淆。You have two problems:
You cannot return data from a
jQuery.each
callback. The return value indicates whether to stop the iteration or not. From the documentation:You cannot return data from an Ajax callback. Ajax is asynchronous, which means in your case that
getLabelsNames
returns before the response is retrieved and processed.Luckily, the autocomplete plugin accepts a function as source. You are already using it, but not correctly. Make it accept the second argument, which is a callback. From the documentation:
So what you have you have to do is passing this callback along to
getLabelsNames
so that it can be called in thesuccess
method of the Ajax call:Note that I use
jQuery.map
[docs] here, where returning a value form the callback has a different meaning. Don't confuse it withjQuery.each
.this
取决于调用的上下文。所以在你的内部函数中,this
是不一样的。根据 dataObj 的结构,您可能想要执行以下操作:
this
depends on the context of the call. SO in your inner function,this
isn't the same.Depending on how your dataObj is structured, you may want to do something like:
AJAX 调用是异步的,这意味着您触发它们,它们稍后响应。您不能简单地从 ajax 调用“返回”一个值——您需要在“成功”时将一个值传递给另一个函数。
AJAX calls are asynchronous, meaning you fire them and they respond LATER. You can't simply "return" a value from an ajax call -- you need to pass a value to another function on "success".