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.
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.
How can I make synchronous Ajax call using Alfresco.util.Ajax?
Following is my code and configuration which I have done.
I have done some configuration shar-config-custom.xml file added mandatory contraint which is calling below js.
validate1.js
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;
};
variable temp is always return true value.
You need to use the callbacks available on the function as these are called after the operation has completed. You have one failureCallback and one successCallback.
Here is an example of a JavaScript jsonPost function using a successCallback:
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"
});
If you want to block further code then you need to overwrite the relevant part of Alfresco and move the calls to the callback.
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
}