Liferay ajax request and JSON response

对着背影说爱祢 提交于 2019-12-05 10:39:17

You can send person id from jsp using ajax request and you can fetch data inside controller(serveResource method) i.e

You need to create URL

<portlet:resourceURL var="resourceURL" >
</portlet:resourceURL>

Ajax Call

$.ajax({
  url : '${resourceURL}',
  data : data,//person id to sent
  type: 'POST',
  dataType : "json",
  success : function(data) {
    // get person data back from controller
  }
});

Controller part

public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) {

    //get person id using getparameter 

    JSONObject json = JSONFactoryUtil.createJSONObject();

    json.put("jsonData", "person details");
    response.getWriter().write(json.toString());

}

HTH

public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) {

    Map<String, Object> data = new HashMap<String, Object>();
    data.put("test1", "test1test1test1test1test1");

    super.writeJSON(request, response, new Gson().toJson(data));
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!