title: How to return an object in response using ajax in liferay portlet?
body: <p>I need to return a string on success method. 
  Here is my AJAX code in jsp page.</p>
  
  <pre><code>  &lt;portlet:actionURL name="filterDocuments" var="filterDocuments"&gt;
      &lt;portlet:param name="action" value="filterDocuments"/&gt;
  &lt;/portlet:actionURL&gt;
  
  &lt;script type="application/javascript"&gt;
      var filterDto = {
          page: 0,
          tags: {},
          search: null
      };
      $.ajax({
          type: "POST",
          url: "&lt;%=filterDocuments%&gt;",
          data: filterDto,
          success: function (msg) {
              alert(msg);
          }
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>And here is my handler on backend side.</p>
  
  <pre><code>@ActionMapping(params = "action=filterDocuments")
      public void filterDocuments(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException {
          PrintWriter writer = PortalUtil.getHttpServletResponse(actionResponse).getWriter();
          writer.write("text");
          writer.flush();
          writer.close();
      }
  </code></pre>
  
  <p>And in result it shows me an empty string in alert message.
  How to deal with it?</p>
  
  <p><strong>UPDATE</strong>
  I changed controller method to</p>
  
  <pre><code> @ResourceMapping(value = "filterDocuments")
  public void filterDocuments(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException {
      PrintWriter writer = PortalUtil.getHttpServletResponse(actionResponse).getWriter();
      writer.write("text");
      writer.flush();
      writer.close();
  }
  </code></pre>
  
  <p>And url at my jsp is</p>
  
  <pre><code>    &lt;portlet:resourceURL var="ajaxResourceURL"/&gt;
  
  &lt;script type="application/javascript"&gt;
      var filterDto = {
          page: 0,
          tags: {},
          search: null
      };
      $.ajax({
          type: "POST",
          url: "&lt;%=ajaxResourceURL%&gt;",
          data: filterDto,
          success: function (msg) {
              alert(msg);
          }
      });
  &lt;/script&gt;
  </code></pre>
  
  <p>And now it gives me an exception </p>
  
  <blockquote>
    <p>org.springframework.web.portlet.NoHandlerFoundException: No handler found for portlet request: mode 'view', phase 'RESOURCE_PHASE', parameters map['search' -> array[''], 'page' -> array['0']]</p>
  </blockquote>
  
  <p>Full controller class</p>
  
  <pre><code>  @Controller
  @RequestMapping("VIEW")
  @Log4j
  public class SpringMVCController extends MVCPortlet {
  
  
  
  
      @RenderMapping
      public String view(RenderRequest request, RenderResponse response) {
  
  
          String view = PropertiesConfig.getAsString("views", request);
  
          return view;
      }
  
  
      @Override
      public void serveResource(ResourceRequest resourceRequest,
                                ResourceResponse resourceResponse)
              throws IOException, PortletException {
          try {
              PrintWriter writer = resourceResponse.getWriter();
              writer.write("text");
              writer.flush();
              writer.close();
              CaptchaUtil.serveImage(resourceRequest, resourceResponse);
          } catch (Exception e) {
              log.error(e.getMessage());
          }
      }
  
  
      @ResourceMapping(value = "action=filterDocuments")
      public void filterDocuments(ActionRequest actionRequest, ActionResponse actionResponse) throws IOException {
          PrintWriter writer = PortalUtil.getHttpServletResponse(actionResponse).getWriter();
          writer.write("text");
          writer.flush();
          writer.close();
      }
  
  
  }
  </code></pre>