1

I'm trying to pass a value in an AJAX 'GET' request to my Portlet class, but I can't quite figure out a way to access such variable's value in the class. My AJAX code is:

function loadXMLDoc() 
{           
   var nocache = new Date().getTime();
   var xmlhttp=new XMLHttpRequest();
   var url = "${mURL}";
   url += '?cache=' + nocache + '&nRows=' + numberOfRows;   // Generate unique request URL
   xmlhttp.open("GET", url,true);
   xmlhttp.onreadystatechange = function()
   {        
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
      { 
          document.getElementById("contentContainer").innerHTML= xmlhttp.responseText;
      } 
   }    
   xmlhttp.send();
}

Where 'nRows' is the variable I am trying to pass to my Portlet class. The part of my portlet class where I attempt to retrieve the variable's value is:

@Override
 public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException,IOException
 {                      
     /* Returns null */
     String nRows = request.getParameter("nRows");

     response.setContentType("text");           
     response.resetBuffer();
     String htmlContent = htmlInterpreter.getParsedHTML();
     response.getWriter().print(htmlContent);
     response.flushBuffer();                                 
 }

Any ideas about alternatives to access the value of 'nRows' in the serveResource(...) method? Thanks in advance!

Heitor Castro
  • 833
  • 6
  • 9
  • 2
    You have to use the *portlet namespace* as prefix for the parameter names in your URL, otherwise they will be ignored. Have a look here: https://www.liferay.com/web/meera.success/blog/-/blogs/liferay-requires-name-spaced-parameters – Andrea Di Giorgi Jun 12 '15 at 21:11
  • @Heitor Castro, Possible duplicate of http://stackoverflow.com/questions/17592144/respond-to-http-request-with-json-object-in-portlet/17593935#17593935 – Parkash Kumar Jun 15 '15 at 05:12
  • @ParkashKumar In this case, I believe the namespace tags were also an issue, but I do appreciate the alternative resource! Thank you, Andrea, for your answer as well! – Heitor Castro Jun 15 '15 at 12:45
  • Are you using aui tags? – Parkash Kumar Jun 15 '15 at 12:49
  • Actually not, but since I had forgotten to use the 'false' and '' before my parameter, I wasn't able to access my nRows variable, even using the ParamUtil.getString() functionality – Heitor Castro Jun 15 '15 at 12:58

1 Answers1

1

I recommend to use liferay's <portlet:resourceURL var="resourceURL"> </portlet:resourceURL> with AlloyUI instead of using simple xmlhttp function.

Change required namespace parameter in your liferay-portlet.xml

<requires-namespaced-parameters>false</requires-namespaced-parameters>

Add following import

    <%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<portlet:defineObjects />

Change the url to below

url += '?cache=' + nocache + '&<portlet:namespace/>nRows=' + numberOfRows; 

Please find below code to get a parameter using ParamUtil:

@Override
 public void serveResource(ResourceRequest request, ResourceResponse response) throws PortletException,IOException
 {                      
     /* Returns null */
     String nRows = ParamUtil.getString(resourceRequest, "nRows");

     response.setContentType("text");           
     response.resetBuffer();
     String htmlContent = htmlInterpreter.getParsedHTML();
     response.getWriter().print(htmlContent);
     response.flushBuffer();                                 
 }

For more details put your eyes on this link:

http://liferayway.blogspot.in/2013/08/ajaxjsonliferay-example.html

Hope it may help you.!!

Ranjitsinh
  • 1,323
  • 1
  • 11
  • 28