Ajax Call in Liferay 6.2 Portfolio

I am starting with Liferay and I would like to make an ajax call. What I am doing: - I have a button On the jsp page to make an ajax call, the variable is initialized in the javascript code and incremented every time the user clicks the button. this variable is passed to the server, which will be used as a parameter to the function.

Snippet of code

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 i cant get the "message" parameter in ContentAutoUpdateAction.java

+3


source to share


3 answers


Although an old question, but wanted to answer.

You need to enable ajax for the portlet.

<ajaxable>true</ajaxable>

      

in liferay-portlet.xml

Second, you can pass a parameter to ajax with



data: {
paramName1: paramValue,
paramName2: paramValue2
}

      

If you want to link it as a request, you can use

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

      

and then use ajaxURL for the ajax request url.

+1


source


The information I have heard is that this is a known bug. I know a workaround:

HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(resourceRequest);

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

      



Hope it helps.

0


source


Not sure about this:

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

      

You are creating a GET query string, so I would change it to:

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

      

Also, this is not the only way to pass parameters in ajax request, you can also try this way:

A.io.request('<%=updaContentURL%>',{
        dataType: 'json',
        method: 'POST',
        data: {message: '1'}

      

0


source







All Articles