3

I'm beginner with Liferay, and I would Like to make an ajax call. What i am doing is: - I have a button In the jsp page to make the ajax call a variable is initialized in the javascript code, and incremented every time the user clicks on the button. this variable is transmeted to the server to be used as a parameter of a function.

Code Snippet

aui_ajax.jsp:

<input type="button" value = "Ajouter un projet" onClick="<portlet:namespace/>Test();return false;">
<div id="usersTable">
  <input type="button" value = "Ajouter un projet" onClick="   <portlet:namespace/>Test();return false;">
</div>
<div id="wait" style="display:none; width: 69px; height: 89px; border: 1px solid black; position: absolute; top: 50%; left: 50%; padding: 2px;">
 <img src='http://www.w3schools.com/jquery/demo_wait.gif' width="64" height="64" />  <br>Loading..
</div>

<script>
var i=1

function <portlet:namespace/>Test() {
    var i=1;
    AUI().use('aui-base','aui-io-request', function(A){
        i++;
        A.one('#wait').setStyle('display', 'block');
        var allRows=A.one('#usersTable').getHTML();

        var querystring = 'message:' + '1';

        //aui ajax call to get updated content
        A.io.request('<%=updaContentURL%>',{
        dataType: 'json',
        method: 'GET',
        data: querystring,
        on: {
             success: function() {
                var data=this.get('responseData');
                A.Array.each(data, function(obj, idx){


                    var tableRow=obj.firstName;
                    //allRows=allRows.trim()+tableRow.trim();



                    //A.one('#usersTable').empty();
                    //A.one('#usersTable').setHTML(tableRow.trim());
                  A.one('#usersTable').append(tableRow.trim());

                    A.one('#wait').setStyle('display','none');
                });
            }
        }
        });
        });
  }
</script>

ContentAutoUpdateAction.java

public class ContentAutoUpdateAction extends MVCPortlet {


public void serveResource(ResourceRequest resourceRequest,
        ResourceResponse resourceResponse)
    throws  IOException, PortletException {

    try {
        System.out.println("====serveResource========");
        JSONObject jsonUser=null;
        JSONArray usersJsonArray=JSONFactoryUtil.createJSONArray();

        resourceRequest.getParameter("message");
        int i=Integer.parseInt(message);    
        i++;
        System.out.println(i);



        //}
        PrintWriter out=resourceResponse.getWriter();
        System.out.println(usersJsonArray.toString());
        out.print(usersJsonArray.toString());
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

the problem is that I can't get the parameter "message" in the ContentAutoUpdateAction.java

Origineil
  • 3,108
  • 2
  • 12
  • 17
user3845254
  • 61
  • 1
  • 7
  • See [this question](http://stackoverflow.com/questions/25052479/how-to-capture-the-data-sent-by-alloy-ui-io-request-in-serveresource-method#comment39138536_25052479) – Origineil Aug 06 '14 at 14:10
  • you might want to add information (e.g. code) on how you get to your `<%=updaContentURL%>` to the question, or did I miss this? And you might need to prefix `` to the parameter name `message` to make Liferay aware that this parameter is directed to your portlet. – Olaf Kock Oct 27 '14 at 16:21

3 Answers3

1

Although an old question but wanted to answer.

You have to enable ajax for the portlet.

<ajaxable>true</ajaxable>

within liferay-portlet.xml

Secondly, you can pass the parameter in ajax by using

data: {
paramName1: paramValue,
paramName2: paramValue2
}

If you want to concatenate it as a querystring then you can use

var ajaxURL = "<portlet:resourceURL/>";
ajaxURL = ajaxURL + "&message=1";

and then use the ajaxURL for the url of the ajax request.

muasif80
  • 5,586
  • 4
  • 32
  • 45
0

The information I have heard is that it's a known bug. I know a workaround:

HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(resourceRequest);

param = PortalUtil.getOriginalServletRequest(httpReq).getParameter("name");

Hope it helps.

rink.attendant.6
  • 44,500
  • 61
  • 101
  • 156
0

Not sure about this:

var querystring = 'message:' + '1';

You are constructing a GET request string, so I would change it to:

var querystring = '&message=' + '1';

Also, that's not the only way to pass parameters to ajax request, you can also try this way:

A.io.request('<%=updaContentURL%>',{
        dataType: 'json',
        method: 'POST',
        data: {message: '1'}
Artem Khojoyan
  • 857
  • 1
  • 6
  • 11