如何在 alfresco 中调用同步 ajax 调用? [英] How to call synchronous ajax call in alfresco?

查看:25
本文介绍了如何在 alfresco 中调用同步 ajax 调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个自定义属性,比如 XYZ.我想在 blur 事件上验证它.我正在使用 JavaScript(客户端)中的 Ajax 调用一个 web 脚本,但它不等待服务器响应并执行进一步的代码.

I have one custom property say XYZ. I want to validate it on blur event. I am calling one web-script using Ajax from JavaScript(Client) but its not waiting for server response and execute further code.

我正在使用 alfresco 自定义验证.我已经完成了有关自定义验证的所有编码,并且工作正常.问题只是脚本没有等待响应并继续执行进一步的代码.

I am using alfresco custom validation. I have done all coding regarding custom validation and it works fine. The issue is only script is not waiting for response and continue to execute further code.

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

How can I make synchronous Ajax call using Alfresco.util.Ajax?

以下是我已经完成的代码和配置.

Following is my code and configuration which I have done.

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

  1. I have done some configuration shar-config-custom.xml file added mandatory contraint which is calling below js.

validate1.js

validate1.js

Alfresco.forms.validation.validate1 = 函数validate1(字段、参数、事件、表单、无声、消息){var temp = true;

Alfresco.forms.validation.validate1 = function validate1( field, args, event, form, silent, message) { 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;
};

变量 temp 总是返回真值.

variable temp is always return true value.

推荐答案

您可以模拟 Alfresco AJAX 同步和异步,如下所示:

You can simulate an Alfresco AJAX both sync and async as follows:

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
}

这篇关于如何在 alfresco 中调用同步 ajax 调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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