Get the AJAX parameters on the server

Eloy Fernández, modified 11 Years ago. New Member Posts: 8 Join Date: 1/6/15 Recent Posts
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:

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) &amp;&amp; resourceId.length() != 0 &amp;&amp; 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);
});
thumbnail
David H Nebinger, modified 11 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
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.
Eloy Fernández, modified 11 Years ago. New Member Posts: 8 Join Date: 1/6/15 Recent Posts
Yes... thank you. It was the namespacing, I sove it making this:


jQuery.ajax({
	url:url,
	type:'POST',
	dataType: "json",
	data:{"<portlet:namespace />operators" : operators},
	success: function(data)
	{
		jQuery('#result').html(data.result);
	}
});


Thank you.
thumbnail
Prakash Khanchandani, modified 11 Years ago. Expert Posts: 329 Join Date: 2/10/11 Recent Posts
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.
Eloy Fernández, modified 11 Years ago. New Member Posts: 8 Join Date: 1/6/15 Recent Posts
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.
thumbnail
David H Nebinger, modified 11 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
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.
thumbnail
Prakash Khanchandani, modified 11 Years ago. Expert Posts: 329 Join Date: 2/10/11 Recent Posts
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 emoticon). Good features for moderation, can clean-up questions and answers for future visitors.

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.