user285519
user285519

Reputation: 13

Liferay Ajax without Jquery

I am trying to get json from the backend of my liferay project and can not even manage to get serveResource to print something to the console. So my ajax request does not do anything in the backend, but still calls success. I am using liferay 6.1.

I tried using jquery with a $.ajax but that was saying ajax method was undefined (even with a full download of jquery-3.2.1), so I think jquery is having issues with multiple includes or something bizarre. I am now using aui Every time I implement aui it will run the ajax function and constantly return success, even though it never ran anything on the backend and this.get("responseData") is null

view.jsp:

<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<script src="https://code.jquery.com/jquery-3.2.1.js"></script>

<portlet:defineObjects />
<%@ taglib uri="http://liferay.com/tld/aui" prefix="aui"%>


<portlet:resourceURL var="saveData" id="saveData" ></portlet:resourceURL>

<br/>

<aui:form>
          <aui:input type="text" name="Name" id="studentName"/>
          <aui:input type="text" name="Email" id="studentEmail"/>
          <aui:input type="text" name="ContactNo" id="studentPhone"/>
          <aui:button type="button" name="saveButton"  value="Save" onclick="save();" />
</aui:form>
<aui:script>
function save(){
    AUI().use('aui-base','aui-io-request', function(A){

        var name=A.one("#<portlet:namespace />studentName").get('value');
        var email=A.one("#<portlet:namespace />studentEmail").get('value');
        var contact=A.one("#<portlet:namespace />studentPhone").get('value');

         A.io.request('<%=saveData%>',{
             dataType: 'json',
             method: 'POST',
             data: { <portlet:namespace/>name: name,
                      <portlet:namespace/>email: email,
                      <portlet:namespace/>contact: contact},
             on: {
             success: function() {
                 var data=this.get('responseData');
                 alert("Success");
                 // Actions to be performed on success
                 }
             }
         });
    });
}
</aui:script>

Menu.java (backend):

import java.io.IOException;
import java.io.PrintWriter;

import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.ProcessAction;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;

/**
 * Portlet implementation class Menu
 */
public class Menu extends GenericPortlet {

    public void init() {
        viewTemplate = getInitParameter("view-template");
    }

    public void doView(
            RenderRequest renderRequest, RenderResponse renderResponse)
        throws IOException, PortletException {

        include(viewTemplate, renderRequest, renderResponse);
    }

    protected void include(
            String path, RenderRequest renderRequest,
            RenderResponse renderResponse)
        throws IOException, PortletException {

        PortletRequestDispatcher portletRequestDispatcher =
            getPortletContext().getRequestDispatcher(path);

        if (portletRequestDispatcher == null) {
            _log.error(path + " is not a valid include");
        }
        else {
            portletRequestDispatcher.include(renderRequest, renderResponse);
        }
    }

    protected String viewTemplate;


    private static Log _log = LogFactoryUtil.getLog(Menu.class);

    /**
     * Custom methods
     * @throws IOException 
     */

    @Override
    public void serveResource(ResourceRequest resourceRequest,
            ResourceResponse resourceResponse) throws IOException,
            PortletException {

        System.out.println("this will not print");
        PrintWriter out = resourceResponse.getWriter();
        out.println("This is sample Text");
    out.flush();    
    super.serveResource(resourceRequest, resourceResponse);
    }

    @ProcessAction(name="saveData")
    public void saveData(ResourceRequest resourceRequest,ResourceResponse resourceResponse){
        System.out.println("This won't print");
        String name = resourceRequest.getParameter("name");
        String emailId = resourceRequest.getParameter("email");
        String contact = resourceRequest.getParameter("contact");
     }    
}

I would like my serveResource function to return JSON to my .jsp file using ajax.

Upvotes: 1

Views: 1266

Answers (1)

Daniele Baggio
Daniele Baggio

Reputation: 2257

Sorry but there are many bad mistakes in your approach:

Good luck

Upvotes: 0

Related Questions