cancel
Showing results for 
Search instead for 
Did you mean: 

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

4535992
Star Collaborator
Star Collaborator

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 ACCEPTED ANSWER

krutik_jayswal
Elite Collaborator
Elite Collaborator

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 answer in original post

2 REPLIES 2

krutik_jayswal
Elite Collaborator
Elite Collaborator

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>

 

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