简体   繁体   English

从外部函数获取Alfresco.util.Ajax.request response.json数据

[英]get Alfresco.util.Ajax.request response.json data from external function

I have an alfresco webscript who return a json response. 我有一个露天剧本,它返回json响应。

I have a js function getWorkflowRepositoryContent() who call this webscript and get the data retuned in the response. 我有一个js函数getWorkflowRepositoryContent() ,该函数调用此webscript并在响应中重新调整数据。

I store the response.json in an array list . 我将response.json存储在数组list

All works fine for me, but when i call getWorkflowRepositoryContent() from another js function, it returned an empty array when it must return an array containing the data received from webscript response . 一切对我来说都很好,但是当我从另一个js函数调用getWorkflowRepositoryContent()时,当它必须返回包含从webscript response接收到的数据的数组时,它返回了一个空数组。

There is the function where i return the data received from the webscript. 有功能,我可以返回从网页脚本接收到的数据。

Can you tell me what i made a mistake, or tell me how to properly return the data from that function. 您能告诉我我做错了什么,还是告诉我如何正确地从该函数返回数据。

function getWorkflowRepositoryContent(){
    var list=[];
    var workflowFilesNameAndNodeRef;

    var test=function getWorkflowFilesList(response)
    {
       workflowFilesNameAndNodeRef=response.json.nodes;
       $.each(response.json.nodes,function(index,value){
           list.push(value.name); 
       });

    }

    Alfresco.util.Ajax.request(
     {
        method:Alfresco.util.Ajax.GET, 
        url: Alfresco.constants.PROXY_URI + "/ALFRESCO-DIRECTORY",
        successCallback:
        {
           fn:test,
             scope:this
        },
        failureCallback:
        {
           fn: function(response)
           {
               Alfresco.util.PopupManager.displayMessage({text:"Failure"});
            },
           scope: this
        }
     });
    console.log(list.length);
   return list;
}  

Your getWorkflowRepositoryContent is getting asynchronous data but returning synchronously so your example won't work. 您的getWorkflowRepositoryContent正在获取异步数据,但会同步返回,因此您的示例将无法正常工作。

An easy way would be to simple call your function with a callback argument. 一种简单的方法是使用回调参数简单地调用您的函数。

function getWorkflowRepositoryContent(cb){ // pass a callback as an argument
    var list=[];
    var workflowFilesNameAndNodeRef;

    var test=function getWorkflowFilesList(response)
    {
        workflowFilesNameAndNodeRef=response.json.nodes;
       console.log(response.json.nodes);
       $.each(response.json.nodes,function(index,value){
           list.push(value.name); 
       });


        $.each(list,function(index, fileName){
            $('<option/>').val(fileName).html(fileName).appendTo('#saveButton');
            $('<option/>').val(fileName).html(fileName).appendTo('#loadButton');
         });
         cb(list); // call the callback once the work is done
    }

    Alfresco.util.Ajax.request(
     {
        method:Alfresco.util.Ajax.GET, 
        url: Alfresco.constants.PROXY_URI + "/ALFRESCO-DIRECTORY",
        successCallback:
        {
           fn:test,
             scope:this
        },
        failureCallback:
        {
           fn: function(response)
           {
               Alfresco.util.PopupManager.displayMessage({text:"Failure To get StarXpert Workflow content"});
            },
           scope: this
        }
     });
}  

getWorkflowRepositoryContent( function(list) {

    console.log(list);
});

You could also use promises but it might be a little harder if you're not familiar with them. 您也可以使用Promise,但是如果您不熟悉Promise ,可能会更加困难。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM