阿贾克斯同步回调 [英] Ajax synchronous callbacks

查看:204
本文介绍了阿贾克斯同步回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pageTest.html在本地文件夹,这个页面调用service.ashx我=?(即返回值参数顺利通过增加+1)与跟随阿贾克斯code:?

I have a pageTest.html in local folder,this page call a service.ashx?i=...(that return value param passed incremented +1) with follow Ajax code:

.
.
getIncr: function(parameters, success){
       $.ajax({
               async: false, 
               type: methodType,
               url: getTarget,
               data: "n="+parameters,
               dataType:"jsonp",
               success: success
             });
    }
.
.

HTML页面调用这个函数的米计时(与脚本..):

The html page call this function for m time (with script..):

  .    
  var start = function(){
  .
  .
  var val = 0;
  .      
  .
  for(i=0; i<m; i++)
  {
      Foo.getIncr(val, function(returned_n){
          val = returned_n;
      });

  }

};

在阿贾克斯:

在页面加载时,调用假 异步执行中的异步模式,但我的设置。我读到这个问题,并找到了原因,那就是阿贾克斯不能同步从page.html即可调用如果在不同的域service.ashx。 是否有执行的page.html即可同步调用该service.ashx溶液(在不同的域)?

During the page loading, the calls are execute in "Asynchronous mode" but i setting "async: false" in Ajax. I read about this problem and found the reason, that is Ajax can't synch call from page.html to service.ashx if there are in different domain. Is there a solution for execute Synch call in page.html to that service.ashx (in different domain)?

推荐答案

好了,问题是,你不能让同步JSONP请求。它的实现方式是,安迪指出,通过一个所谓的脚本标签黑客。有没有办法jQuery的可以在等待脚本标记要填充停止的JavaScript应用程序的执行。

Well, the problem is that you cannot make synchronous JSONP requests. The way it is implemented is, as Andy pointed out, through a so-called "script tag hack". There is no way jQuery can stop the execution of the javascript application while waiting for a script tag to be filled.

所以,您所呼叫的jQuery的正确的方法做一个JSONP请求,但因为它是JSONP,异步选项不应用,有没有办法解决它。你必须异步处理请求。

So you are calling the right method of jQuery to make a JSONP request but because it is JSONP, the asynchronous option is not applied and there is no way around it. You have to handle the request asynchronously.

在一个侧面说明,它不是使用$就在同步模式的时候是个好主意。如果请求需要太多的时间才能完成,浏览器将停止机会是你的用户会得到一个弹出说的页面不响应。

On a side note, it is not a good idea to use $.ajax in synchronous mode anyway. If the request takes too much time to be completed, the browser will stall and chances are your user will get a popup saying the page is not responding.

这篇关于阿贾克斯同步回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆