如何在调用$ .get方法时使javascript变量成为全局变量? [重复]

时间:2022-09-13 00:37:51

Possible Duplicate:
How can I get jQuery to perform a synchronous, rather than asynchronous, AJAX request?
Get a variable after ajax done

可能重复:如何让jQuery执行同步而非异步的AJAX请求? ajax完成后获取变量

The two alert functions in the following code returns different results. I am trying to get the second to evaluate true too.. Any help is appreciated.. Thanks..

以下代码中的两个警报函数返回不同的结果。我正试图让第二个评估也是真的..任何帮助都表示赞赏..谢谢..

var array;

  $.get('php/getstocklist.php', function(data){  

  array = data; 
  alert($.isArray(array));    //alerts true

  }, "json");

alert($.isArray(array));      //alerts false

5 个解决方案

#1


1  

The problem is that the $.get is async. The var is global as you have it now. It just isn't defined because your second alert runs immediately before the ajax has returned and run its success callback.

问题是$ .get是异步的。 var现在是全局的。它只是未定义,因为您的第二个警报在ajax返回之前立即运行并运行其成功回调。

In order to use it outside the the callback you would need to poll a variable to make sure the result has been assigned... but then you are essentially using a fake callback so it makes more sense to restructure your code and do it the normal way :-)

为了在回调之外使用它,你需要轮询一个变量以确保已经分配了结果......但是你实际上是在使用一个虚假的回调,所以更有意义的是重构你的代码并按正常方式进行办法 :-)

#2


0  

Here's what's happening:

这是发生了什么:

// This variable _is_ global. However, it's current value is undefined
var array;

$.get('php/getstocklist.php', function(data){  

  // After this runs, _now_ array has data in it
  array = data; 

  // thus causing $.isArray to return true here.
  alert($.isArray(array));    //alerts true

}, "json");

// This call happens BEFORE the callback to the $.get call,
// so as of this line, array is still undefined, which results in 
// the $.isArray call returning false.
alert($.isArray(array));      //alerts false

Javascript is a heavy asynchronous language. You have to kind of break away from procedure thinking, and instead think in terms of passing bits of code around to be executed later (those are callbacks).

Javascript是一种重型异步语言。你必须脱离程序思维,而是考虑传递一些代码以便稍后执行(那些是回调)。

#3


0  

The second alert executes before the first because the $.get callback executes asynchronously. If you want the second alert to return true, you should use a jQuery Deferred object like so:

第二个警报在第一个警报之前执行,因为$ .get回调是异步执行的。如果您希望第二个警报返回true,则应使用jQuery Deferred对象,如下所示:

var array;

deferred = $.get('php/getstocklist.php', function(data){  

  array = data; 
  alert($.isArray(array));    //alerts true

}, "json");

// alerts true if $.get succeeded
deferred.done(function () { alert($.isArray(array)); }); 

The callback you pass to done will fire when the asynchronous $.get call returns.

当异步$ .get调用返回时,您传递给done的回调将触发。

#4


0  

If I understand you right why can't you

如果我理解你,你为什么不能

$.getJSON('php/getstocklist.php', function(data){  
    dosomething(data);
});


function dosomething(data) {}

#5


-3  

Its a bit off a fudge but try setting using that will break your variable out off the request and make it global

它有点偏软,但尝试使用它会将你的变量从请求中分离出来并使其成为全局变量

window.array = data;  

alert($.isArray(window.array));

#1


1  

The problem is that the $.get is async. The var is global as you have it now. It just isn't defined because your second alert runs immediately before the ajax has returned and run its success callback.

问题是$ .get是异步的。 var现在是全局的。它只是未定义,因为您的第二个警报在ajax返回之前立即运行并运行其成功回调。

In order to use it outside the the callback you would need to poll a variable to make sure the result has been assigned... but then you are essentially using a fake callback so it makes more sense to restructure your code and do it the normal way :-)

为了在回调之外使用它,你需要轮询一个变量以确保已经分配了结果......但是你实际上是在使用一个虚假的回调,所以更有意义的是重构你的代码并按正常方式进行办法 :-)

#2


0  

Here's what's happening:

这是发生了什么:

// This variable _is_ global. However, it's current value is undefined
var array;

$.get('php/getstocklist.php', function(data){  

  // After this runs, _now_ array has data in it
  array = data; 

  // thus causing $.isArray to return true here.
  alert($.isArray(array));    //alerts true

}, "json");

// This call happens BEFORE the callback to the $.get call,
// so as of this line, array is still undefined, which results in 
// the $.isArray call returning false.
alert($.isArray(array));      //alerts false

Javascript is a heavy asynchronous language. You have to kind of break away from procedure thinking, and instead think in terms of passing bits of code around to be executed later (those are callbacks).

Javascript是一种重型异步语言。你必须脱离程序思维,而是考虑传递一些代码以便稍后执行(那些是回调)。

#3


0  

The second alert executes before the first because the $.get callback executes asynchronously. If you want the second alert to return true, you should use a jQuery Deferred object like so:

第二个警报在第一个警报之前执行,因为$ .get回调是异步执行的。如果您希望第二个警报返回true,则应使用jQuery Deferred对象,如下所示:

var array;

deferred = $.get('php/getstocklist.php', function(data){  

  array = data; 
  alert($.isArray(array));    //alerts true

}, "json");

// alerts true if $.get succeeded
deferred.done(function () { alert($.isArray(array)); }); 

The callback you pass to done will fire when the asynchronous $.get call returns.

当异步$ .get调用返回时,您传递给done的回调将触发。

#4


0  

If I understand you right why can't you

如果我理解你,你为什么不能

$.getJSON('php/getstocklist.php', function(data){  
    dosomething(data);
});


function dosomething(data) {}

#5


-3  

Its a bit off a fudge but try setting using that will break your variable out off the request and make it global

它有点偏软,但尝试使用它会将你的变量从请求中分离出来并使其成为全局变量

window.array = data;  

alert($.isArray(window.array));