Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Pranoti Nandurkar
How to call ajax in portlet
May 15, 2013 11:18 PM
Answer

Pranoti Nandurkar

Rank: Junior Member

Posts: 48

Join Date: February 2, 2012

Recent Posts

Hi,

I have to give ajax call in portlet. How can I do this?
Dharmen Panchal
RE: How to call ajax in portlet
May 15, 2013 11:30 PM
Answer

Dharmen Panchal

Rank: New Member

Posts: 13

Join Date: February 13, 2013

Recent Posts

Hi Pranoti Nandurkar,

Try this code.

 1
 2<portlet:resourceURL var="ajaxURL"></portlet:resourceURL>
 3
 4function ajaxCall<portlet:namespace/>(parameter){
 5       $.ajax({
 6               url : '${ajaxURL}',
 7               data : {
 8                       param:parameter
 9               },
10
11               type: 'POST',
12               dataType : "json",
13               success : function(data) {
14                       // do stuff on success
15                       // data consists the response sent by server.
16               },
17               error: function () {
18                    //do stuff on error
19                    console.log('Error Occurred');
20                }
21       });
22}


This ajax call will hit the serveResource() method in Portlet class (controller).

Let me know if you have any difficulties.
Bart Simpson
RE: How to call ajax in portlet
May 15, 2013 11:30 PM
Answer

Bart Simpson

Rank: Liferay Master

Posts: 524

Join Date: August 29, 2011

Recent Posts

You can write alloy code to make a call.
http://www.liferay.com/web/nathan.cavanaugh/blog/-/blogs/alloyui-working-with-ajax
Tejas Kanani
RE: How to call ajax in portlet
May 15, 2013 11:31 PM
Answer

Tejas Kanani

Rank: Liferay Master

Posts: 655

Join Date: January 6, 2009

Recent Posts

You can create resourceURL(same as actionURL/renderURL) in your jsp and it will take you to serveResource method of your controller where you can write your logic.
vijay pandey
RE: How to call ajax in portlet
May 15, 2013 11:33 PM
Answer

vijay pandey

Rank: Junior Member

Posts: 44

Join Date: September 1, 2010

Recent Posts

Hi Pranoti Nandurkar

You can use this ways to using the ajax call with jquery .

<portlet:resourceURL var="fetchJSPUrl"></portlet:resourceURL>
jQuery.ajax({
type: "POST",
url: "<%=renderResponse.encodeURL(fetchJSPUrl.toString())%>",
cache: false,
dataType: "html",
success: function(data)
{
jQuery(data).insertBefore(".list_closer");
}
});
}

if any issue please let me know

Cheers
Vijay Pandey
vijay pandey
RE: How to call ajax in portlet
May 15, 2013 11:39 PM
Answer

vijay pandey

Rank: Junior Member

Posts: 44

Join Date: September 1, 2010

Recent Posts

Pranoti Nandurkar:
Hi,

I have to give ajax call in portlet. How can I do this?


sample portlet on Ajax-JQuery here is the http://sourceforge.net/projects/ajaxjqueryportl/
Pranoti Nandurkar
RE: How to call ajax in portlet
May 15, 2013 11:43 PM
Answer

Pranoti Nandurkar

Rank: Junior Member

Posts: 48

Join Date: February 2, 2012

Recent Posts

Thanks for Quick reply.

I have to pass some values to serveResource, so how can I append it in Javascript / ajax function.

--
Pranoti
Tejas Kanani
RE: How to call ajax in portlet
May 15, 2013 11:49 PM
Answer

Tejas Kanani

Rank: Liferay Master

Posts: 655

Join Date: January 6, 2009

Recent Posts

While making ajax call you can pass parameter in data field.

1data : {
2        param1:value1,
3        param2:value2
4},


and in serverResource you can get it via

1final String value1 = resourceRequest.getParameter("param1");
Dharmen Panchal
RE: How to call ajax in portlet
May 15, 2013 11:50 PM
Answer

Dharmen Panchal

Rank: New Member

Posts: 13

Join Date: February 13, 2013

Recent Posts

Dharmen Panchal:
Hi Pranoti Nandurkar,

Try this code.

 1
 2<portlet:resourceURL var="ajaxURL"></portlet:resourceURL>
 3
 4function ajaxCall<portlet:namespace/>([b]parameter[/b]){
 5       $.ajax({
 6               url : '${ajaxURL}',
 7              [b] data : {
 8                       param:parameter
 9               }, [/b]
10
11               type: 'POST',
12               dataType : "json",
13               success : function(data) {
14                       // do stuff on success
15                       // data consists the response sent by server.
16               },
17               error: function () {
18                    //do stuff on error
19                    console.log('Error Occurred');
20                }
21       });
22}


This ajax call will hit the serveResource() method in Portlet class (controller).

Let me know if you have any difficulties.




In the javascript function ajaxCall<portlet:namespace/>() I have passed an argument named "parameter".

this is as param request parameter. In the serveResource() method you can get this parameter value by
request.getParameter("param")

If you want to pass more parameters with this request something like this:
data : {
param1 : parameter1,
param2 : parameter2,
param3 : parameter3
}

you will get the data by calling request.getParameter("param<n>") in the serveResource() method.
Samita Bhujbal
RE: How to call ajax in portlet
May 15, 2013 11:55 PM
Answer

Samita Bhujbal

Rank: Regular Member

Posts: 120

Join Date: July 5, 2011

Recent Posts

Hi pranoti,
There are two ways to give ajax call :-
1] Old method :-
var xmlHttp;
var xmlHttp;
function showdegree(){

if (typeof XMLHttpRequest != "undefined"){
xmlHttp= new XMLHttpRequest();
}
else if (window.ActiveXObject){
xmlHttp= new ActiveXObject("Microsoft.XMLHTTP");
}
if (xmlHttp==null){
alert("Browser does not support XMLHTTP Request");
return;
}
var str=document.getElementById("degFac").value;
var url="/MembershipModule-portlet/html/degreelist.jsp";
url +="?count=" +str;
xmlHttp.onreadystatechange = degreeChange;
xmlHttp.open("GET", url, true);
xmlHttp.send(null);
}

function degreeChange(){

if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){
document.getElementById("GradDegreeIEDiv").innerHTML=xmlHttp.responseText;
}
}



2]
$("#emailid").blur(function()
{

var PrimaryEmailID = $("#emailid").val();
var dataString4 = 'function=checkEmail'+'&PrimaryEmailID='+ PrimaryEmailID;
if(PrimaryEmailID.length>0)
{
$.ajax({

url: "<liferay-portlet:resourceURL></liferay-portlet:resourceURL>",
data: dataString4,
cache: false,
success: function(html)
{
if(html!='')
{
alert(html);
$('#emailid').val("");
$('#emailid').focus();
}else {

}
}
}); return false;
}
});

Yogesh Kumar
RE: How to call ajax in portlet
October 16, 2013 10:30 AM
Answer

Yogesh Kumar

Rank: New Member

Posts: 9

Join Date: June 3, 2013

Recent Posts

Could you please give me the solution of this query.

<select id="provinceId" name="provinceId"
onchange="changeCity(this.value);">
<option value=""><%=LanguageUtil.get(pageContext, "Spec-Cat-Name")%></option>
<%
for (SpecCategory specCatObj : specCatList) {
%>
<option value="<%=specCatObj.getSpecCatId()%>"><%=specCatObj.getSpecCatName()%></option>
<%
}
%>
</select>
<select id="cityId" name="cityId">
<option value=""><%=LanguageUtil.get(pageContext, "Specifi-Name")%></option>
</select>

<script type="text/javascript>
<portlet:actionURL var="methodURL" name="showStatCityAjax"
windowState="<%=LiferayWindowState.EXCLUSIVE.toString()%>" />

<script src="<%=request.getContextPath()%>/js/jquery.js"></script>
<script type="text/javascript">

function changeCity(provinceId)
{
<%-- var url="<%=request.getContextPath().concat(showStatCityAjax.toString())%>"+"?provinceId="+provinceId; --%>
var url='<%=methodURL%>';
alert("URL="+url);
alert("id = " +provinceId);
var id = document.getElementById('cityId');
var controlId = id.options[id.selectedIndex].text;
init(url, controlId);
}


</script>
-----------------------------------------------------------
portlet class is:

public void showStatCityAjax(ActionRequest request , ActionResponse response) throws NumberFormatException, SystemException{
System.out.println("Hello");
String specCantName = null;
String specName = null;
long specId;
String message = "*" + "" + "#";
String provinceId = request.getParameter("provinceId");
//String provinceId = ParamUtil.getString(request, "provinceId");
System.out.println("Province Id =" +provinceId);

Here this is printing Province Id is null and i am able to show NullPointerException
please guide me.
Sujay Kumar Paul
RE: How to call ajax in portlet
January 14, 2014 5:52 PM
Answer

Sujay Kumar Paul

Rank: Regular Member

Posts: 165

Join Date: October 28, 2011

Recent Posts

Hi,

Can any one please tell me how to get the value of data from struts 2 action class.

Thanks in advance.
Bharamani P Tashildar
RE: How to call ajax in portlet
June 23, 2015 11:59 PM
Answer

Bharamani P Tashildar

Rank: Junior Member

Posts: 35

Join Date: August 27, 2014

Recent Posts

Hi everyone


Please tell me how can i get entity details in javascript?
Is it possible to use localserviceutil in script tag????

Thanks
Bharamani T
Pavel Savinov
RE: How to call ajax in portlet
June 24, 2015 12:05 AM
Answer

Pavel Savinov

LIFERAY STAFF

Rank: Junior Member

Posts: 38

Join Date: May 29, 2015

Recent Posts

Bharamani P Tashildar:
Please tell me how can i get entity details in javascript?
Is it possible to use localserviceutil in script tag????


Take a look:
Understanding ServiceBuilder and JavaScript for Liferay

P. 6 Javascript access