JavaScript的:Ajax请求后,全局变量全局变量、JavaScript、Ajax

2023-09-10 16:02:26 作者:Insist(坚持)

这个问题是非常简单的技术:

  VAR it_works = FALSE;

$。员额(some_file.php,'',功能(数据){

     it_works =真;

});

警报(it_works); #假的(是的,警报已经来到这里,而不是在$。员额本身)
 

我想实现的是:

 警报(it_works); # 真正
 

有没有办法做到这一点?如果不能够 $。员额()返回被应用到 it_works

值 解决方案 JavaScript jQuery AJAX JSON这个之间的关系

您的期望是在同步(阻止的)类型的请求。

  VAR it_works = FALSE;

jQuery.ajax({
  键入:POST,
  网址:some_file.php,
  成功:功能(数据){
    it_works =真;
  },
  异步:假//<  - 这把它变成同步
});

//执行被阻塞,直到请求完成。

// it_works可用
警报(it_works);
 

请求是同步(非阻塞的),通过默认这意味着浏览器不会等他们按顺序完成继续开展工作。这就是为什么你的戒备了错误的结果。

现在,随着 jQuery.ajax 你可以选择设置请求为同步,这意味着该脚本只能继续运行的 的请求完成后。

在推荐的方式,然而,就是为重构您code,这样的数据将被传递到回调功能一旦完成请求。这是因为pferred阻断执行装置阻断这是不能接受在UI $ P $。做这种方式:

  $。员额(some_file.php,'',功能(数据){
    iDependOnMyParameter(数据);
});

功能iDependOnMyParameter(参数){
    //你应该做你的工作在这里,依赖于请求的结果!
    警报(参数)
}

//所有code这里应该是独立的AJAX请求的结果
// ...
 

  

异步编程稍微复杂,因为后果   做出请求的被封装在一个函数,而不是以下的请求语句。 但是实时行为在用户体验可以是显著   好,因为他们不会看到一个缓慢的服务器或网络不畅导致   浏览器作为,虽然它已经坠毁。 同步编程不敬   和不应该在所使用的应用程序的人使用的

道格拉斯·克罗克福德的 (锐志)的

the question is fairly simple and technical:

var it_works = false;

$.post("some_file.php", '', function(data) {

     it_works = true;

});

alert(it_works); # false (yes, that 'alert' has to be here and not inside $.post itself)

What I want to achieve is:

alert(it_works); # true

Is there a way to do that? If not can $.post() return a value to be applied to it_works?

解决方案

What you expect is the synchronous (blocking) type request.

var it_works = false;

jQuery.ajax({
  type: "POST",
  url: 'some_file.php',
  success: function (data) {
    it_works = true;
  }, 
  async: false // <- this turns it into synchronous
});​

// Execution is BLOCKED until request finishes.

// it_works is available
alert(it_works);

Requests are asynchronous (non-blocking) by default which means that the browser won't wait for them to be completed in order to continue its work. That's why your alert got wrong result.

Now, with jQuery.ajax you can optionally set the request to be synchronous, which means that the script will only continue to run after the request is finished.

The RECOMMENDED way, however, is to refactor your code so that the data would be passed to a callback function as soon as the request is finished. This is preferred because blocking execution means blocking the UI which is unacceptable. Do it this way:

$.post("some_file.php", '', function(data) {
    iDependOnMyParameter(data);
});

function iDependOnMyParameter(param) {
    // You should do your work here that depends on the result of the request!
    alert(param)
}

// All code here should be INDEPENDENT of the result of your AJAX request
// ...

Asynchronous programming is slightly more complicated because the consequence of making a request is encapsulated in a function instead of following the request statement. But the realtime behavior that the user experiences can be significantly better because they will not see a sluggish server or sluggish network cause the browser to act as though it had crashed. Synchronous programming is disrespectful and should not be employed in applications which are used by people.

Douglas Crockford (YUI Blog)