Planned maintenance is scheduled for the week of June 15th - the exact date and time will be announced soon.
See More Details
Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
Get the AJAX parameters on the server
Hello everybody.
I am trying to send an AJAX request to a portlet, and it half works. I show you my code and after explain better:
The jQuery AJAX:
And the serveResource
OK!! What it is working is the response, when I press the input with the id operation the div with id result loads an 8 (that the server response writting on jsonFeed.put("result", 8); The 8 is only for a test). What it is not working is the operators String on _log.info("The data from AJAX are: " + operators); that it is a null (if I use resourceRequest.getParameter("operators");) or an empty string (if I use ParamUtil.getString(resourceRequest, "operators");).
What am I doing wrong? and what can I do to receive this value?
Thank you very much.
PS: On the client side, I tried too this:
I am trying to send an AJAX request to a portlet, and it half works. I show you my code and after explain better:
The jQuery AJAX:
jQuery("#operation").click(function()
{
var url = '<portlet:resourceurl id="getDataResourceURL"></portlet:resourceurl>';
var operators = jQuery('#result').html();
jQuery.ajax({
url:url,
type:'post',
dataType: "json",
data:{operators:operators},
success: function(data)
{
jQuery('#result').html(data.result);
}
});
And the serveResource
@Override
public void serveResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse) throws PortletException, IOException
{
String resourceId = resourceRequest.getResourceID();
if (Validator.isNotNull(resourceId) && resourceId.length() != 0 && resourceId.equalsIgnoreCase("getDataResourceURL"))
{
//final String operators = resourceRequest.getParameter("operators");
String operators = ParamUtil.getString(resourceRequest, "operators");
_log.info("The data from AJAX are: " + operators);
JSONObject jsonFeed = JSONFactoryUtil.createJSONObject();
jsonFeed.put("result", 8);
resourceResponse.setContentType("application/json");
resourceResponse.setCharacterEncoding("UTF-8");
resourceResponse.getWriter().write(jsonFeed.toString());
}
}
OK!! What it is working is the response, when I press the input with the id operation the div with id result loads an 8 (that the server response writting on jsonFeed.put("result", 8); The 8 is only for a test). What it is not working is the operators String on _log.info("The data from AJAX are: " + operators); that it is a null (if I use resourceRequest.getParameter("operators");) or an empty string (if I use ParamUtil.getString(resourceRequest, "operators");).
What am I doing wrong? and what can I do to receive this value?
Thank you very much.
PS: On the client side, I tried too this:
jQuery.getJSON(url, {operators:operators}, function(data)
{
jQuery('#result').html(data.result);
});
If I had to guess I would put my money on namespacing issues...
Namespacing occurs on both the client side as well as on the server side. When an incoming parameter is namespaced correctly, Liferay will include it to pass along to the portlet request. Any parameter not namespaced correctly is not included in the portlet request as it is likely for some other portlet or purpose.
So I'm guessing that the "operators" guy is there on the HTTP request, but it gets dropped on it's way into the portlet as part of the ResourceRequest.
I'd suggest using PortalUtil.getHttpServletRequest(resourceRequest) to get the incoming http request, then look for the "operators" parameter there.
Namespacing occurs on both the client side as well as on the server side. When an incoming parameter is namespaced correctly, Liferay will include it to pass along to the portlet request. Any parameter not namespaced correctly is not included in the portlet request as it is likely for some other portlet or purpose.
So I'm guessing that the "operators" guy is there on the HTTP request, but it gets dropped on it's way into the portlet as part of the ResourceRequest.
I'd suggest using PortalUtil.getHttpServletRequest(resourceRequest) to get the incoming http request, then look for the "operators" parameter there.
Yes... thank you. It was the namespacing, I sove it making this:
Thank you.
jQuery.ajax({
url:url,
type:'POST',
dataType: "json",
data:{"<portlet:namespace />operators" : operators},
success: function(data)
{
jQuery('#result').html(data.result);
}
});
Thank you.
Ah! Cross-posting on different sites. When you cross-post a question on multiple sites please link the question to each other it helps the community.
Thanks for putting the answer though and also as usual David had already answered it before it was done on Stackoverflow.
Thanks for putting the answer though and also as usual David had already answered it before it was done on Stackoverflow.
OK!! I was thinking do it, but I thought that link form a different forum wasn´t a good idea. Next time, I´ll do.
Thanks Prakash.
Thanks Prakash.
I don't monitor SO much at all. I have little if any interest in building a 'point count' in answering questions.
I'm also of the impression that LR has a vibrant community here in the forums, so there is no need to post to SO unless you have a non-Liferay question. I know people want the 'point count' thing for posting questions and answers, and I also know that their interface may be favored over Liferay's, but at the end of the day the Liferay expertise is over here, not over there.
I'm also of the impression that LR has a vibrant community here in the forums, so there is no need to post to SO unless you have a non-Liferay question. I know people want the 'point count' thing for posting questions and answers, and I also know that their interface may be favored over Liferay's, but at the end of the day the Liferay expertise is over here, not over there.
I'm also of the impression that LR has a vibrant community here in the forums
Strongly agree.
so there is no need to post to SO unless you have a non-Liferay question. I know people want the 'point count' thing for posting questions and answers, and I also know that their interface may be favored over Liferay's, but at the end of the day the Liferay expertise is over here, not over there.
Not so much in agreement here. More than the point-count the interface is too good to beat, sleak and its clean - just questions and good-answers unlike hijacking discussions in forum questions (like i am doing here
Liferay expertise is comparatively less but its building up, infact many liferay-staff are members. SO may not be first place, but definitely the second best to get an answer on liferay.
Anyways, thank you so much David for giving your valuable time and support to us.
Community
Company
Feedback