如何在露天调用同步ajax调用?

时间:2015-02-12 09:30:42

标签: ajax validation alfresco

我有一个自定义属性说XYZ。我想在模糊事件上验证它。 我使用JavaScript(客户端)中的Ajax调用一个Web脚本,但它不等待服务器响应并执行更多代码。

我正在使用alfresco自定义验证。我已完成有关自定义验证的所有编码,它工作正常。问题是只有脚本不等待响应并继续执行更多代码。

如何使用 Alfresco.util.Ajax 进行同步Ajax调用?

以下是我的代码和配置。

  1. 我已经完成了一些配置的shar-config-custom.xml文件添加了强制性约束,它在js下面调用。

  2. validate1.js


    Alfresco.forms.validation.validate1 = function validate1(        字段,args,事件,形式,沉默,消息){      var temp = true;

     Alfresco.util.Ajax.jsonGet ({
         url: Alfresco.constants.PROXY_URI + "myscript/getdata?data=" +field.value,
      successCallback:
         {
             fn: function(response)
             {
                 temp = response.json.Result;
                 if (temp === "false") {
                    Alfresco.util.PopupManager.displayMessage({
                    text: "You can not go further"
                    });
                    temp = false;
                 }
             },
             scope: this
      }
     });
     return temp;
    };
    
  3. 变量temp始终返回true值。

2 个答案:

答案 0 :(得分:3)

您需要使用函数上可用的回调,因为在操作完成后会调用这些回调。你有一个failureCallback和一个successCallback。

以下是使用successCallback的JavaScript jsonPost函数的示例:

Alfresco.util.Ajax.jsonPost(
    {
        url: Alfresco.constants.PROXY_URI + "my/webscript?nodeRef=" + nodeRef,
        successCallback: {
            fn: function (o) {

                //Make further calls

            },
            scope: this
        },
        failureMessage: "Server Error"
    });

如果您想阻止更多代码,则需要覆盖Alfresco的相关部分并将调用移至回调。

答案 1 :(得分:2)

您可以按如下方式模拟Alfresco AJAX同步和异步:

function myAlfrescoAjax(wsUrl, async) {
// wsUrl: string, the url of your webscript
// async: boolean, a false value simulates synchronous

    var simSync = {}; // define a local variable for simulation
    Alfresco.util.Ajax.request(
    {
    method : "GET",       
    url: Alfresco.constants.PROXY_URI + wsUrl,
        successCallback: {
            fn: function (response) {
                // insert code for succesful response
                if (!async) clearTimeout(simSync);  // resume execution  when ready (see below)
            },
            scope: this
        },
        failureMessage: "Server Error",
        execScripts: true
    });
// now immediately stop execution (if synchronous) with a timeout that takes longer than you ever expect the time the Ajax call will take
// note that this timer is killed on succes of the response function in the Ajax call
    if (!async) {
        simSync = setTimeout(function(){
        // insert code for the unexpected case the timer elapses fully
        }, 5000);
    }
    return
}