Reputation: 61
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
Upvotes: 3
Views: 3895
Reputation: 6006
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.
Upvotes: 1
Reputation: 857
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'}
Upvotes: 0
Reputation: 31
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.
Upvotes: 0