• Ext.Ajax.request同步请求


    导读:

    ajax分为2种,一种是同步,一种是异步
    同步:代码执行完了之后才执行后面的代码

    异步:代码刚执行,后面的代码就马上接着执行了,不管前面的代码是否执行完
    异步的情况下,要获得返回信息,就需要在异步执行完之后写代码,也就是在success里面写代码,或者success里面调用其他的函数。

    在Ext3.0以上的版本里面 ,Ext.Ajax.request是异步的,到Ext 4.0以后才支持同步请求的属性(没试过) 

    --------------------------------------------------------------------------------------------------------------------------------------------------------------- 

    本人今天遇到的一个问题,其中一个项目中用到Ext3.4里面的Ext.Ajax.request请求,如下所示

        Ext.Ajax.request({
                        url:'tempSaveRuleConfig.action',
                        method:'POST',
                        success:function(result,request)
                        {
                            tcProductDetailStore.modified = [];
                            tcProductDetailStore.reload();
                            var time = getServerTime();
                            if (time == '')
                            {
                                Ext.MessageBox.alert('ERROR','从服务器获取时间失败。');  
                                return;
                            }
                            var win = new Ext.Window({
                                    modal:true,
                                    layout:'fit',
                                    400,
                                    height:150,
                                    closeAction:'close'
                                });    
                                win.show();    
    }

    其中的

    getServerTime()方法也是同样的ajax请求,但在其中加了一个属性, async : false, 想通过同步的方式获取时间后再继续往下判断时间是否为空来往下执行。
    经百度和实验,发现 Ext3.+不支持同步的方法,只支持异步请求,这样当还没返回时间time的值,程序就又往下执行判断时间为空从而报错提示:
    从服务器获取时间失败。
    那么问题来了,怎么去解决呢?
    经过网上大牛指点,可以编写一个同步的请求方法,具体如下所示:
     1          function getServerTime()
     2         {
     3             var obj;
     4             if (window.ActiveXObject) {
     5                 obj = new ActiveXObject('Microsoft.XMLHTTP');
     6             } else if (window.XMLHttpRequest) {
     7                 obj = new XMLHttpRequest();
     8             }
     9             var url = 'takeDptcServerTime.action';
    10             obj.open('POST', url, false);
    11             obj.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    12             obj.send(null);
    13             var time = obj.responseText;
    14             return time;
    15         } 

      OK,经过测试,发现调用以上方法能实现同步的ajax请求。


    具体可以参考这篇博文:
    http://blog.csdn.net/linan0930/article/details/12556703

  • 相关阅读:
    VMware虚拟机网络设置
    Spring4 In Action-7.1.2-自定义DispatcherServlet配置、添加其他的Servlet和Filter
    Spring4 In Action-5.2.3-Spring Web应用程序-向页面输出列表、接收参数、接收表单
    Spring4 In Action-5.2.2-Spring Web应用程序-简单的控制器实现跳转
    Spring4 In Action-4.3-@AspectJ-args-往切面传递参数
    Spring4 In Action-4.2-@AspectJ-切面
    java.lang.IllegalStateException: Failed to load ApplicationContext
    Spring4 In Action-3.5.1-@PropertySource运行时注入值
    Spring4 In Action-3.2@Scope单例、多例Bean
    Spring In Action-3.2@Conditional条件化Bean
  • 原文地址:https://www.cnblogs.com/tanglc/p/4483036.html
Copyright © 2020-2023  润新知