Liferay Portlet上的AJAX [英] AJAX on liferay portlets
问题描述
我正在尝试将AJAX请求发送到Portlet,并且它可以正常工作.我向您展示我的代码,并在进行更好的解释之后:
I am trying to send an AJAX request to a portlet, and it half works. I show you my code and after explain better:
jQuery AJAX:
The jQuery AJAX:
jQuery("#operation").click(function()
{
var url = '<portlet:resourceURL id="getDataResourceURL"></portlet:resourceURL>';
var operators = jQuery('#result').html();
jQuery.ajax({
url:url,
dataType: "json",
data:{operators:operators},
success: function(data)
{
jQuery('#result').html(data.result);
}
});
和serveResource
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());
}
}
好!!它正在工作的是响应,当我使用id操作按输入时,具有id结果的div会加载8(服务器响应写在jsonFeed.put("result",8)上; 8仅用于测试)).它不起作用的是_log.info(来自AJAX的数据是:" +运算符)上的运算符String.它是一个空值(如果我使用resourceRequest.getParameter("operators");)或一个空字符串(如果我使用ParamUtil.getString(resourceRequest,"operators");).
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?
非常感谢您.
PS:在客户端,我也尝试这样做:
PS: On the client side, I tried too this:
jQuery.getJSON(url, {operators:operators}, function(data)
{
jQuery('#result').html(data.result);
});
PS:也发布在 Liferay论坛
PS: Also posted in Liferay forums
推荐答案
是否可以将 operators
更改为 operators1
,使其看起来像 {operators1:operator}
Can you change operators
to say operators1
so it would look like {operators1 : operators}
或
这可能是由于名称空间引起的,也许您可以尝试使用
it might be due to namespace, may be you can try having it like
data: {"<portlet:namespace />operators" : operators}
或
可以尝试在您的 serveResource
方法代码中获取httpRequest,例如:
may be try getting the httpRequest in your serveResource
method code like:
HttpServletRequest request = PortalUtil.getHttpServletRequest(resourceRequest);
String operators = ParamUtil.getString(resourceRequest, "operators");
让我知道其中的任何一项
Let me know if any of this works
这篇关于Liferay Portlet上的AJAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!