Liferay DXP MVCResourceCommand return json response

thumbnail
Aravinth Kumar, modified 5 Years ago. Regular Member Posts: 152 Join Date: 6/26/13 Recent Posts
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.​​​​​​​

@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());
    }
}
thumbnail
David H Nebinger, modified 5 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
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.
thumbnail
Aravinth Kumar, modified 5 Years ago. Regular Member Posts: 152 Join Date: 6/26/13 Recent Posts
Thanks for your quick reply. I m triggering ajax call on button click to fetch some value.
 $.ajax({
              url:"${validateCodeUrl}",
              method:'GET',
              data: {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<portlet:namespace />regNumber: $("#<portlet:namespace />regNumber").val(),
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;},
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;dataType:'json',
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;success:function(customVal){
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log("here its printing the output with html source..");
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.dir(customVal);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#<portlet:namespace />registrationCity").val(customVal.code);
&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}
&nbsp;&nbsp; &nbsp;&nbsp;});
thumbnail
David H Nebinger, modified 5 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
and how is validateCodeUrl derived?
thumbnail
Aravinth Kumar, modified 5 Years ago. Regular Member Posts: 152 Join Date: 6/26/13 Recent Posts
I have created portlet resource url.
<portlet:resourceurl id="validateVehicle" var="validateVehicleUrl" />

thumbnail
David H Nebinger, modified 5 Years ago. Liferay Legend Posts: 14933 Join Date: 9/2/06 Recent Posts
That's not the same as validateCodeUrl; do you have a typo in the JS maybe?
thumbnail
Aravinth Kumar, modified 5 Years ago. Regular Member Posts: 152 Join Date: 6/26/13 Recent Posts
Hi,
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" />

thumbnail
Aravinth Kumar, modified 5 Years ago. Regular Member Posts: 152 Join Date: 6/26/13 Recent Posts
RE: Liferay DXP MVCResourceCommand return json response