【问题标题】:Access Liferay Web services using JSON API使用 JSON API 访问 Liferay Web 服务
【发布时间】:2023-11-23 23:19:01
【问题描述】:

您好,我创建了一个插件 portlet。在 JSP 中,我使用 JSON API 访问所有国家/地区列表。它对登录用户工作正常。但对于访客用户,我无法访问网络服务。我正在使用 Liferay 6.0.6。以下是我的代码。

Liferay.Service.Portal.Country.getCountries(
            {}, 
            function(result) {
                for(var count=0;count< result.length;count++){
                    alert(result[count].name);
                    var option = document.createElement("option");

                }
            }
        );

【问题讨论】:

    标签: web-services liferay portal liferay-6


    【解决方案1】:

    假设你使用的是Liferay 6.1,你可以通过在portal-ext.properties文件中添加一个属性来实现

    json.service.public.methods=getCountries
    

    如果需要检查全流程结帐

    JSONServiceAction
    

    【讨论】:

      【解决方案2】:

      我找到了解决上述问题的方法。我无法访问 JSON API,因为 Liferay 正在使用 A.io.request 进行 AJAX 调用,该调用仅适用于登录用户。所以我准备了以下代码。

      jQuery.ajax({
                          type: "POST",  
                          url: '<%=themeDisplay.getURLPortal() %>'+'/tunnel-web/json?serviceClassName=com.liferay.portal.service.CountryServiceUtil&serviceMethodName=getCountries',
      
                          dataType: 'json',
                          success: function(countriesList) {
                              alert(countriesList);
                              alert(countriesList[0].countryId);
                              }
      
                          }       
                  });
      

      【讨论】:

        【解决方案3】:

        我认为您需要将具有权限的 serviceContext 传递给服务。

        您可以尝试将 communityPermissions 和 guestPermissions 设置为 VIEW 吗?

        Liferay.Service.Portal.Country.getCountries(
                {
        
                serviceContext: jQuery.toJSON(
                    {
                        communityPermissions: communityPermission,
                        guestPermissions: guestPermission,
                        scopeGroupId: themeDisplay.getScopeGroupId()
                    }
                )
        
                }, 
                function(result) {
                    for(var count=0;count< result.length;count++){
                        alert(result[count].name);
                        var option = document.createElement("option");
        
                    }
                }
            );
        

        【讨论】:

        • 我收到一个 javascript 错误,即“对象不支持以下行的属性或方法 'request' 'AUI().io.request(serviceUrl,config);'
        • 下面是语法。确保语法正确。有关更多详细信息,您可以访问此链接。 www.liferay.com/community/wiki/-/wiki/Main/json+service+api Liferay.Service.NAMESPACE.ENTITY.METHOD( { PARAMETER_1: VALUE_1, PARAMETER_2: VALUE_2 }, function(message) { var exception = message .exception; if (!exception) { // 处理成功 } else { // 处理异常 } }
        • 嗨 Sharana,我已经阅读了那篇文章。由于访客用户的 AUI().io.request() 方法不可用,我遇到了问题。感谢您的回复。
        • 好的,我认为您尝试访问的服务可能需要进行身份验证。如果您使用的是本地主机,请尝试在您的门户网站扩展属性中设置此 json.servlet.hosts.allowed=127.0.0.1?或服务器 IP。