Alfresco webscript:AJAX,JSON

时间:2017-10-25 20:45:07

标签: json ajax alfresco alfresco-webscripts

我已成功创建了一个返回JSON响应的webscript。见下文:

get.js文件:

// search for folder within Alfresco content repository
var folder = roothome.childByNamePath("PATH");

// validate that folder has been found
if (folder == undefined || !folder.isContainer) {
   status.code = 404;
   status.message = "Folder " + " not found.";
   status.redirect = true;
}

// construct model for response template to render
model.folder = folder; 

get.json.ftl:

{"corporates" : [
     <@recurse_macro node=folder depth=0/>
     ]
}

<#macro recurse_macro node depth>
     <#list node.children?sort_by(["properties","name"]) as child>
          {
          "Name" : "${child.properties.name}",
          "URL" : "${child.url}",
          "serviceURL" : "${child.serviceUrl}",
          "shareURL" : "${child.shareUrl}",
          "ID" : "${child.id}",
          "Type" : "${child.typeShort}"
          },
          <#if child.isContainer>
          {
               <@recurse_macro node=child depth=depth+1/>
          }
          </#if>
     </#list>
</#macro>

这会彻底返回JSON(哇喔!),但我想使用AJAX从第二个webscript中获取JSON。

目前,我正在我的第二个webscript的get.html.ftl文件中使用典型的AJAX调用,如下所示:

$(document).ready(function() {
    $('.submit-button').click(function(e) {

  // Avoid to trigger the default action of the event.
  e.preventDefault();

  // Actions declared here...
  $.ajax({
      type: 'GET',
      dataType: 'html',
      url: 'PLACEHOLDER_URL_PATH',
      success: function(data) {

        // Shows the result into the result panel.
        $('#alfresco-result').html(data);
        alert('Done.');

      },
      error: function(data) {

        // Shows the result into the result panel.
        $('#alfresco-result').html("ERROR");
        }
    });
});
})

我的问题是为什么当我使用dataType时AJAX调用不起作用:&#39; json&#39;?

我想在我的AJAX调用中解析JSON并将其转换为html(例如html列表),但它不接受JSON dataType作为可接受的输入。

感谢任何帮助!

1 个答案:

答案 0 :(得分:0)

你可以使用像这样的ajax使用POST Webscript Call并传递你的jsonObject

  

dataObj:&#34; yourJsonObject&#34;,

到dataObject

Alfresco.util.Ajax.jsonPost(

{
            url: Alfresco.constants.PROXY_URI + "sample/mypostwebscript",

            dataObj:"yourJsonObject",

            successCallback: {

                            fn: function(res){

                               alert("success");

                               alert(res.responseText);                                                                                            

                            },

                            scope: this

            },

                            failureCallback:

                            {

                               fn: function(response)

                               {

                                              // Display error message and reload

                                              Alfresco.util.PopupManager.displayPrompt(

                                              {

                                                             title: Alfresco.util.message("message.failure", this.name),

                                                             text: "search failed"

                                              });                                          

                               },

                                   scope: this

                                }

});

},