I want get parameters from json, so I have a code:
@RenderMode(name = "VIEW")
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws PortletException, IOException {
JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
resourceRequest.setAttribute("resourceUrl", "http://localhost:8080/");
jsonObject.put("rowsPerPage", 10);
jsonObject.put("page", 1);
jsonObject.put("total", 100);
PrintWriter writer = resourceResponse.getWriter();
writer.write(jsonObject.toString());
}
What should be written in JavaScript method that displays the: rowsPerPage, page, total
I have a JavaScript code:
$.getJSON('http://localhost:8080/', function(jd) {
alert(jd.rowsPerPage);
});
JSP page:
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<portlet:defineObjects />
This is the <portlet:resourceURL var="testURL" id="view.jsp" escapeXml="false" />
<br/><br/>
<div id="myGrid" style="width:600px;height:500px;"></div>
<div id="pager" style="width:600px;height:20px;"></div>
But it is wrong, why?
Check your URL
http://localhost:8080/if it returns a JSON response (you can directly paste the URL in the address bar of the Browser). This would return the Welcome page of Liferay.For calling the
serveResourcemethod you would require aresourceURLto be passed in the jQuery functiongetJSONwhich can be generated using the<liferay-portlet:resourceURL>or<portlet:resourceURL>tags in JSP.Update:
You should use the
resourceURLin the javascript:Also I would like to suggest to make sure that the
serveResourcemethod is actually called with the script by included some log statements in the method.Here are some links for examples as to how to use Ajax in Liferay:
Hope this helps.