Liferay ajax request, Cannot get parameter at serveResource method

thumbnail
Rahul Panchivala, modified 10 Years ago. New Member Posts: 14 Join Date: 1/21/15 Recent Posts
Hi all,

I have trouble in getting parameter in serveResource method ,
My scenario is : I have one view.jsp page and one separate abc.js file
I got portletNamespace and resourceURL by setting
<input type="hidden" id="portletNameSpace" value="<portlet:namespace/>"/>
<input type="hidden" id="portletResourceURL" value="<portlet:resourceURL/>"/>
in my view.jsp file
and get this property into abc.js file by writing this
var portletNameSpace=$('#portletNameSpace').val();
var portletResourceURL=$('#portletResourceURL').val();

now my problem is :
why am i not getting any parameter in my serveResource method
I have below code.

var getHospitalId = portletNameSpace+"getHospitalId";
var getHospitalistId = portletNameSpace+"getHospitalistId";
var getCalendarProfile = portletNameSpace+"getCalendarProfile";
var getCheckedList = portletNameSpace+"getCheckedList";
A.io.request(portletResourceURL,{
dataType: 'json',
data:{getHospitalId : hospitalId,
getHospitalistId : hospitalistId,
getCalendarProfile : calendarProfile,
getCheckedList : checkedList
},
method:'post',
on:{
success: function(){}
}
});
Note : I got call in serveResource method but not getting any parameters that i set in data field
siddhant jain, modified 10 Years ago. Junior Member Posts: 69 Join Date: 3/19/13 Recent Posts
Hi Rahul,

you can go through these links on how to ajax in liferay:

link1.
link2

Thanks
Siddhant
thumbnail
Rahul Panchivala, modified 10 Years ago. New Member Posts: 14 Join Date: 1/21/15 Recent Posts
Hi siddhant jain,

Thanks for the reply,
I had already applied this solutions but however i have no results.
thumbnail
David H Nebinger, modified 10 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
Namespace doesn't work like that Rahul.

When you namespace an input field, the natural name is decorated with instance information so there is no DOM name collisions.

if you have two of your portlet on a page, then the <input type="hidden" id="portletNameSpace" value="<portlet:namespace/>"/> concept fails as you cannot get a unique DOM element when you search by id so your value would end up being wrong.

Your inputs must be namespaced to guarantee uniqueness and also to provide visibility on the portlet (the portal filters out any parameters that are not namespaced from list in the PortletRequest, and it also trims the namespace prefix off so your code doesn't have to worry about it).

So you're submitting a request with param portletNameSpace, but because it is not namespaced it doesn't get fed to the portlet's serveResource method.
thumbnail
Rahul Panchivala, modified 10 Years ago. New Member Posts: 14 Join Date: 1/21/15 Recent Posts
Hii David H Nebinger,

Thanks for the reply David,
Do u have any suggetion for me how to apply namespace before bold text below..
data:{
getHospitalId : hospitalId,
getHospitalistId : hospitalistId,
getCalendarProfile : calendarProfile,
getCheckedList : checkedList
}
thumbnail
David H Nebinger, modified 10 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
If possible, don't use ids at all. Decorate with custom classes and use the class selector to access elements. Doesn't help with the issue if you have instanceable portlets, but it's better than working with the ids.
thumbnail
Gurumurthy Godlaveeti, modified 10 Years ago. Regular Member Posts: 208 Join Date: 8/12/11 Recent Posts
Hi Rahul,

After seeing your code, i would like to give two suggestions here.

1) Print all variables data in JS before adding to ajax request.
2) The data section will have data in key:value format. The key is your own name & the value is static value or JS variable names.
You must use key names in serveResource() method for getting right values.

You have JS variable names something like getHospitalId but inside data section, its different.

better to use like below.

data{
getHospitalId : getHospitalId ,
getHospitalistId : getHospitalistId ,
getCalendarProfile : getCalendarProfile ,
getCheckedList : getCheckedList
}

This time, it will give your values in serveResource method also.

Don't forget to check values of those parameters before adding to ajax request.

Thanks
Guru
thumbnail
Rahul Panchivala, modified 10 Years ago. New Member Posts: 14 Join Date: 1/21/15 Recent Posts
hii Gurumurthy Godlaveet,
Thanks for the reply, i follow your steps but i didn't succeeded,
i applied below code

var getHospitalId = portletNameSpace+"getHospitalId";
var getHospitalistId = portletNameSpace+"getHospitalistId";
var getCalendarProfile = portletNameSpace+"getCalendarProfile";
var getCheckedList = portletNameSpace+"getCheckedList";

A.io.request(portletResourceURL,{
dataType: 'json',
data:{
getHospitalId : hospitalId,
getHospitalistId : hospitalistId,
getCalendarProfile : calendarProfile,
getCheckedList : checkedList
}
thumbnail
Sushil Patidar, modified 10 Years ago. Expert Posts: 467 Join Date: 10/31/11 Recent Posts
Hi,

To get request parameters through ajax request ,. you need to set following property in liferay-portlet.xml.

<requires-namespaced-parameters>false</requires-namespaced-parameters>


Regards
thumbnail
Marco Azzalini, modified 6 Years ago. Regular Member Posts: 146 Join Date: 11/18/14 Recent Posts
Rahul Panchivalahii Gurumurthy Godlaveet,
Thanks for the reply, i follow your steps but i didn't succeeded,
i applied below code

var getHospitalId = portletNameSpace+"getHospitalId";
var getHospitalistId = portletNameSpace+"getHospitalistId";
var getCalendarProfile = portletNameSpace+"getCalendarProfile";
var getCheckedList = portletNameSpace+"getCheckedList";

A.io.request(portletResourceURL,{
dataType: 'json',
data:{
getHospitalId : hospitalId,
getHospitalistId : hospitalistId,
getCalendarProfile : calendarProfile,
getCheckedList : checkedList
}
Hi Rahul, long time has passed since your post :-)
I found myself in your same situation so I can add a little note that could help someone else.
Your code would work as is, provided one change the 'data type' value to  'text/html' instead of 'json'. In this way ParamUtil.getXXXX methods will work as expected in the java code.