JQuery 未定义响应

发布于 2024-10-01 01:10:38 字数 567 浏览 4 评论 0原文

我认为这非常简单并且记录过多,但我已经连续几个小时在这上面,但我就是不明白!我想要做的就是返回 $.getJSON 查询的结果。如果我将它附加到 div 等,它工作得很好,但如果我尝试获取结果,我可以在回调函数中提醒它,但不能在其他地方提醒它。我认为我的代码将显示我想要做的事情:

var thumb = 
              $.getJSON('http://localhost/mapScripts/getThumbs.php?thumbnails=?',
function(data) { 
alert(data); //shows the result perfect!
var thumb = data; 
return thumb;

});

alert(thumb); //undefined

提前抱歉,如果这看起来是一个愚蠢的问题,但我一直无法得到它:(

非常感谢您的时间,

elshae

编辑 梅德和大卫都给了我很好的答案。我想将两者都标记为我的答案,但似乎我只能选择一个:-/。遇到此问题的任何人,请查看这两个答案:)

I have the notion that this is very easy and over-documented, but I have been at this for hours on end and I just don't get it! All I want to do is return the result of a $.getJSON query. It works fine if I append it to a div etc, but if I try to just get the result, I can alert it in the callback function, but not anywhere else. I think my code will show what I am trying to do:

var thumb = 
              $.getJSON('http://localhost/mapScripts/getThumbs.php?thumbnails=?',
function(data) { 
alert(data); //shows the result perfect!
var thumb = data; 
return thumb;

});

alert(thumb); //undefined

Sorry in advance if it seems like such a silly question, but I haven't been able to get it :(

Many thanks for your time,

elshae

EDIT
Meder and David both gave me great answers. I'd like to mark both as my answer, but it seems I can only choose one :-/. Anyone having this issue, please look at both answers :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

瞄了个咪的 2024-10-08 01:10:39

Ajax 是异步的。无需依赖 thumb,只需在回调中调用函数并执行相同的功能即可。

var thumb = data;
anotherFunction( thumb );

否则,如果坚持使用此结构,请指定 async:false,尽管这会破坏 Ajax 的全部优势。

Ajax is asynchronous. Instead of relying on thumb just call a function in the callback and do the same functionality.

var thumb = data;
anotherFunction( thumb );

Otherwise, if are insistent upon this structure, specify async:false though that kills the whole benefit of Ajax.

绮烟 2024-10-08 01:10:39

您已经进行了几次不同的尝试来获取此处的数据,但由于不同的原因,它们都失败了。我稍后会解释原因,首先是解决方案:

无论您对数据做什么都必须在回调函数中完成。

现在,为什么您的尝试不起作用:

您可以' t 从 Ajax 请求返回数据。

Ajax(通常)通过创建 XHR 对象、为“请求返回时”设置事件处理程序,然后发送 HTTP 请求来工作。

您无法从中返回数据,其原因与您无法从中返回事件对象的原因相同:

var not_the_event = jQuery('foo').click(function (event) { return event; });

您无法从函数内部设置名为 thumb 并在函数外部定义的变量的值使用 var 关键字定义 thumb 的函数。 var 关键字创建作用域为函数的变量的新实例。

如果您将 var 从回调函数中取出,它仍然无法工作,因为您会遇到计时问题。

如果您有:

var not_the_event;
jQuery('foo').click(function (event) { not_the_event = event; });
alert(not_the_event);

那么您将收到 undefined 警报,因为调用 alert 时尚未发生点击,因此事件处理尚未设置该变量功能。

使用 Ajax,到达警报时 HTTP 请求尚未得到答复,因此它也是未定义的。

这让我们回到:无论你对数据做什么,都必须在回调函数中完成。

回调函数的要点是当数据准备好时调用它。因此,无论您对数据做什么,都可以完成。

You've made a couple of different attempts to get the data here, and they fail for different reasons. I'll explain why in a moment, first though — the solution:

Whatever you do with the data has to be done in the callback function.

Now, why your attempts don't work:

You can't return data from an Ajax request.

Ajax works (usually) by creating an XHR object, setting an event handler for "When the request comes back", and then sending an HTTP request.

You can't return data from it for the same reason that you can't get the event object returned from:

var not_the_event = jQuery('foo').click(function (event) { return event; });

You can't set the value of a variable called thumb and defined outside a function from inside a function that defines thumb using the var keyword. The var keyword creates a new instance of the variable that is scoped to the function.

If you took the var away from the callback function, it still wouldn't work because you would have timing issues.

If you had:

var not_the_event;
jQuery('foo').click(function (event) { not_the_event = event; });
alert(not_the_event);

Then you would be alerting undefined because the click hasn't taken place when the alert is called, so the variable hasn't been set by the event handling function.

With Ajax, the HTTP request hasn't been answered by the time the alert is reached, so it is also undefined.

Which brings us back to: Whatever you do with the data has to be done in the callback function.

The point of the callback function is that it is called when the data is ready. So whatever you do with the data can be done then.

苏别ゝ 2024-10-08 01:10:39

您在本地定义了它,如果您希望它在全局范围内工作,请在全局范围内定义它:

var thumb = {};
$.getJSON('http://localhost/mapScripts/getThumbs.php?thumbnails=?',
function(data) { 
alert(data); //shows the result perfect!
thumb = data; 

});

alert(thumb);

尽管正如其他人所发布的那样,最好将回调传递给另一个函数,因为依赖全局变量是一种不好的做法。并且警报会在 ajax 完成之前触发。

you defined it locally, if you want it to work globally, define it globally:

var thumb = {};
$.getJSON('http://localhost/mapScripts/getThumbs.php?thumbnails=?',
function(data) { 
alert(data); //shows the result perfect!
thumb = data; 

});

alert(thumb);

Though as others have posted it's better to have the call back pass it to another function since relying on globals is a bad practice. And the alert would trigger before the ajax was done.

缱绻入梦 2024-10-08 01:10:39

首先,不要在内部作用域中定义第二个同名变量:)

关于您的问题,请在调用“$.getJSON”之前添加以下行:

$.ajaxSetup({ async: false });

First of all don't define second variable with the same name in inner scope :)

Regarding to your problem add following line before you call '$.getJSON':

$.ajaxSetup({ async: false });

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文