7*4 2015-01-05 16:18 采纳率: 50%
浏览 33

Liferay Portlet上的AJAX

I am trying to send an AJAX request to a portlet, and it half works. I show you my code and after explain better:

The jQuery AJAX:

jQuery("#operation").click(function() 
{
    var url         = '<portlet:resourceURL id="getDataResourceURL"></portlet:resourceURL>';
    var operators   = jQuery('#result').html();
    jQuery.ajax({
        url:url,
        dataType: "json",
        data:{operators:operators},
        success: function(data)
        {
            jQuery('#result').html(data.result);
        }
});

And the serveResource

@Override
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws PortletException, IOException 
{
    String resourceId = resourceRequest.getResourceID();
    if (Validator.isNotNull(resourceId) && resourceId.length() != 0 && resourceId.equalsIgnoreCase("getDataResourceURL"))
    {
        //final String operators = resourceRequest.getParameter("operators");
        String operators = ParamUtil.getString(resourceRequest, "operators");

        _log.info("The data from AJAX are: " + operators);

        JSONObject jsonFeed = JSONFactoryUtil.createJSONObject();


        jsonFeed.put("result", 8);
        resourceResponse.setContentType("application/json");
        resourceResponse.setCharacterEncoding("UTF-8");
        resourceResponse.getWriter().write(jsonFeed.toString());
    }
}

OK!! What it is working is the response, when I press the input with the id operation the div with id result loads an 8 (that the server response writting on jsonFeed.put("result", 8); The 8 is only for a test). What it is not working is the operators String on _log.info("The data from AJAX are: " + operators); that it is a null (if I use resourceRequest.getParameter("operators");) or an empty string (if I use ParamUtil.getString(resourceRequest, "operators");).

What am I doing wrong? and what can I do to receive this value?

Thank you very much.

PS: On the client side, I tried too this:

jQuery.getJSON(url, {operators:operators}, function(data) 
{
    jQuery('#result').html(data.result);
});

PS: Also posted in Liferay forums

  • 写回答

1条回答 默认 最新

  • Memor.の 2015-01-06 07:22
    关注

    Can you change operators to say operators1 so it would look like {operators1 : operators}

    or

    it might be due to namespace, may be you can try having it like

    data: {"<portlet:namespace />operators" : operators}
    

    or

    may be try getting the httpRequest in your serveResource method code like:

    HttpServletRequest request = PortalUtil.getHttpServletRequest(resourceRequest);
    String operators = ParamUtil.getString(resourceRequest, "operators");
    

    Let me know if any of this works

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line