【问题标题】:Alfresco webscript: AJAX, JSONAlfresco 网页脚本:AJAX、JSON
【发布时间】:2017-10-26 13:27:04
【问题描述】:

我已经成功创建了一个返回 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 从第二个 web 脚本中获取 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: 'json' 时 AJAX 调用不起作用?

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

感谢任何帮助!

【问题讨论】:

  • 当您在浏览器的开发工具中查看响应时,AJAX 调用会返回什么?
  • 嘿@JeffPotts,感谢您的回复。我最终意识到我的 JSON webscript 没有构建有效的 JSON 响应(哎呀!)。 AJAX 调用在创建有效的 JSON 响应后完美运行。

标签: json ajax alfresco alfresco-webscripts


【解决方案1】:

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

dataObj:"yourJsonObject",

到数据对象

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

                                }

});

},

【讨论】:

    猜你喜欢
    • 2011-05-21
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多