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
RE: Liferay DXP MVCResourceCommand return json response
Hi Friends,I m using mvresourcecommand in liferay dxp to use multiple serve resource but it's returning complete html source + expected output back to jsp success function. Did anyone face this issue?If I go with normal serveresource in controller its returning back the expected json output alone.
Please find the code snippet below.Please check and let me know if any modification required. Thanks in Advance.
Please find the code snippet below.Please check and let me know if any modification required. Thanks in Advance.
@Component(property = { "javax.portlet.name=" + liferay_TestPortlet,
"mvc.command.name=validateCode" }, service = MVCResourceCommand.class)
public class VehTransaction extends BaseMVCResourceCommand {
@Override
protected void doServeResource(ResourceRequest resourceRequest, ResourceResponse resourceResponse)
throws Exception {
JSONObject registrationObject = JSONFactoryUtil.createJSONObject();
registrationObject.put("code", "test");
resourceResponse.setContentType("application/json");
resourceResponse.getWriter().write(registrationObject.toString());
}
}
Your code is not much different from a standard Liferay implementation: https://github.com/liferay/liferay-portal/blob/7.2.x/modules/apps/product-navigation/product-navigation-personal-menu-web/src/main/java/com/liferay/product/navigation/personal/menu/web/internal/portlet/action/GetPersonalMenuItemsMVCResourceCommand.java#L58-L88
This tends to point more towards how you are invoking the serve resource request instead of how the request handler itself is implemented.
This tends to point more towards how you are invoking the serve resource request instead of how the request handler itself is implemented.
Thanks for your quick reply. I m triggering ajax call on button click to fetch some value.
$.ajax({
url:"${validateCodeUrl}",
method:'GET',
data: {
<portlet:namespace />regNumber: $("#<portlet:namespace />regNumber").val(),
},
dataType:'json',
success:function(customVal){
console.log("here its printing the output with html source..");
console.dir(customVal);
$("#<portlet:namespace />registrationCity").val(customVal.code);
}
});
and how is validateCodeUrl derived?
I have created portlet resource url.
<portlet:resourceurl id="validateVehicle" var="validateVehicleUrl" />
That's not the same as validateCodeUrl; do you have a typo in the JS maybe?
Hi,
Thanks for your response. No its not a typo mistake in my js. Resource URL is same as the validateCodeUrl.
Thanks for your response. No its not a typo mistake in my js. Resource URL is same as the validateCodeUrl.
<portlet:resourceurl id="validateCode" var="validateCodeUrl" />
RE: Liferay DXP MVCResourceCommand return json response
Copyright © 2025 Liferay, Inc
• Privacy Policy
Powered by Liferay™