RE: ajax call to serve resource..

Lucian Alexandrescu, modified 11 Years ago. New Member Posts: 10 Join Date: 4/16/14 Recent Posts
Hi everyone!

I make this call from view.jsp and is working perfect..

<portlet:resourceurl id="addEntry" var="addUrl" />

<input type="button" value="Add Ajax Call" onclick="ajaxAddEntry('${addUrl}');">

function ajaxAddEntry(addUrl) {	
		
	jQuery.ajax({
 			type: 'GET',
			 url:  addUrl,
			 cache:false,
			 async:true,
			 error: function(){
						 alert('error');
				    },		
			dataType: "text",
			data :{
				data1: "123",
				data2: "456",				
 				},
 			success: function(status) {			
				 alert('response : ' + status);
			 }
		});
}



public class MyPortlet extends MVCPortlet {	

    @Override
    public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
    		throws PortletException {        	
    	
		 try {
			 String resourceID = resourceRequest.getResourceID();

			 if (resourceID.equals("addEntry")) {
				
				System.out.println("here test addEntry");
				
				addEntry(resourceRequest, resourceResponse);
			}
...

}


public JSONObject addEntry(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
			throws Exception {

          String data1 = resourceRequest.getParameter("bookmarkUrl");

}





How can I achieve the same result calling the method from portal theme?

Thanks in advance for any suggestion!
thumbnail
Pankaj Kathiriya, modified 11 Years ago. Liferay Master Posts: 722 Join Date: 8/5/10 Recent Posts
If you are asking about calling your portlet's serveResource method outside of your portlet, then use <liferay-portlet:resourceURL/> (using taglib uri "http://liferay.com/tld/portlet")

For example: <liferay-portlet:resourceURL portletName="NAMEOFPORTLET" var="addUrl"/>
thumbnail
Mohd Danish, modified 11 Years ago. Regular Member Posts: 187 Join Date: 3/5/12 Recent Posts
As far as I know you cant make ajax call from theme. To make ajax calls you need to have a portlet in your theme.
Make your ajax call from portlet, and embed it into portal theme.
thumbnail
N. Belo, modified 7 Years ago. Junior Member Posts: 33 Join Date: 3/17/09 Recent Posts

Well I think it's possible to make an Ajax Call from the Theme if you have defined a Friendly URL for a portlet and then do a POST or GET to that URL.
Don't you agree?