Ajax Implementation in the Liferay


Creating the Resource URL

Liferay's default uses the resource url for ajax requests

         <portlet:resourceURL id="addResource" var="addResourceURL">

        </portlet:resourceURL>


Creating the Ajax function

creating the Jquery  ajax function

function <portlet:namespace/>getEntries(){

                var url = "${addResourceURL}";

                        $.ajax({
                                type : "POST",
                                url : url,
                                dataType : "JSON",
                                success : function(data) {
                                        if(data.Exist){
                                             
                                        }
                                },
                                error : function(XMLHttpRequest, textStatus, errorThrown) {
                                     
                                }
                        });
                }




Creating the Serve Resource method ;

creating the ajax implemention method , As of now we are creating the JSON Object and assigning the  values to the Objects so that based on the Object values we keep we can iterate the html elements.

public void addResource (ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws SystemException  {

                    JSONObject jsonObject = JSONFactoryUtil.createJSONObject(); // Creating the JSON Object.
                   jsonObject.put("Exist", "This Page is already Exists"); // Adding Objects to the JSON
                   jsonObject.put("isPageExist", "The Page Already Exists.");
                     
                try {
                        PrintWriter writer = resourceResponse.getWriter();
                        writer.write(jsonObject.toString());
                } catch (IOException e) {
                        log.error("Error in adding the quicklink", e);
                }

Comments

Popular posts from this blog

Theme display in javascript

How to know which liferay version we are using

Viewing the SQL Query in liferay : debugging the SQL Query in Hibernate