How can i make a synchronous request with Alfresco.util.ajax ?

cancel
Showing results for 
Search instead for 
Did you mean: 
4535992
Senior Member

How can i make a synchronous request with Alfresco.util.ajax ?

Jump to solution

Hi i must call a webscript on the repo tier , and wait for is response so i'm need to make a synchronous request (a no ajax request), i'm finding hard time to make this with the api javascript of alfresco, i just don't want to import some external library like jquery  or alfresco-js-api for resolved this issue , there is some function under lafresco 5 for make this happen with alfresco script api.

here a piece of code of my problem:

<script type="text/javascript">//<![CDATA[
YAHOO.util.Event.onContentReady("${fieldHtmlId}", function ()
{
   var myVar = "";
    Alfresco.util.Ajax.jsonGet({
        url: encodeURI(Alfresco.constants.PROXY_URI + '/myserviceuri'),
        successCallback:
        {
            fn: function loadWebscript_successCallback(response, config)
            {
                var obj = Alfresco.util.parseJSON(response.serverResponse.responseText);
                if (obj)
                {
                    myVar = obj;
                }
            }
        }
    });
    //Now i made a second call but the first call has not set already the myVar variable
    Alfresco.util.Ajax.jsonGet({
        url: encodeURI(Alfresco.constants.PROXY_URI + '/myserviceotheruri?rock='+myVar[key]),
        successCallback:
        {
            fn: function loadWebscript_successCallback(response, config)
            {
                var obj = Alfresco.util.parseJSON(response.serverResponse.responseText);
                if (obj)
                {
                   console.log("SUCCESS:" +obj);
                }else{
                   console.error("myVar is null or empty");
                }
            }
        }
    });
}, this);
//]]></script>


How can i made the first call a not ajax call with the native javascript api of alfresco? without involving some external library?
1 Solution

Accepted Solutions
krutik_jayswal
Senior Member II

Re: How can i make a synchronous request with Alfresco.util.ajax ?

Jump to solution

Making synchronous request is not good practice, I suggest you should change the logic of your code.This kind of situation comes in many cases.Taking the synchronous is not good.

May be changing logic as below will do the magic for you.

<script type="text/javascript">//<![CDATA[
YAHOO.util.Event.onContentReady("${fieldHtmlId}", function ()
{
    Alfresco.util.Ajax.jsonGet({
        url: encodeURI(Alfresco.constants.PROXY_URI + '/myserviceuri'),
        successCallback:
        {
            fn: function loadWebscript_successCallback(response, config)
            {
                var obj = Alfresco.util.parseJSON(response.serverResponse.responseText);
                if (obj)
                {
                                //Make the second call here
     Alfresco.util.Ajax.jsonGet({
        url: encodeURI(Alfresco.constants.PROXY_URI + '/myserviceotheruri?rock='+obj[key]),
        successCallback:
        {
            fn: function loadWebscript_successCallback(response, config)
            {
                var obj = Alfresco.util.parseJSON(response.serverResponse.responseText);
                if (obj)
                {
                   console.log("SUCCESS:" +obj);
                }else{
                   console.error("myVar is null or empty");
                }
            }
        }
    });

                }
            }
        }
    });
   

}, this);
//]]></script>

 

View solution in original post

2 Replies
krutik_jayswal
Senior Member II

Re: How can i make a synchronous request with Alfresco.util.ajax ?

Jump to solution

Making synchronous request is not good practice, I suggest you should change the logic of your code.This kind of situation comes in many cases.Taking the synchronous is not good.

May be changing logic as below will do the magic for you.

<script type="text/javascript">//<![CDATA[
YAHOO.util.Event.onContentReady("${fieldHtmlId}", function ()
{
    Alfresco.util.Ajax.jsonGet({
        url: encodeURI(Alfresco.constants.PROXY_URI + '/myserviceuri'),
        successCallback:
        {
            fn: function loadWebscript_successCallback(response, config)
            {
                var obj = Alfresco.util.parseJSON(response.serverResponse.responseText);
                if (obj)
                {
                                //Make the second call here
     Alfresco.util.Ajax.jsonGet({
        url: encodeURI(Alfresco.constants.PROXY_URI + '/myserviceotheruri?rock='+obj[key]),
        successCallback:
        {
            fn: function loadWebscript_successCallback(response, config)
            {
                var obj = Alfresco.util.parseJSON(response.serverResponse.responseText);
                if (obj)
                {
                   console.log("SUCCESS:" +obj);
                }else{
                   console.error("myVar is null or empty");
                }
            }
        }
    });

                }
            }
        }
    });
   

}, this);
//]]></script>

 

4535992
Senior Member

Re: How can i make a synchronous request with Alfresco.util.ajax ?

Jump to solution

 very simple answer  for a very stupid question, ty very much