AJAX on liferay portlets

限于喜欢 提交于 2021-02-11 11:52:07

问题


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:


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



来源:https://stackoverflow.com/questions/27783552/ajax-on-liferay-portlets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!