2 Replies Latest reply on Nov 13, 2017 10:20 AM by larryking11

    JQuery AJAX calls within portlet jsp pages

    abdulbasitmughal

      I have tried to add JQuery AJAX calls in jsp page within portlet in the following way ..

       

      1- Added code into JSP page

      <script type="text/javascript">

                              jQuery(document).ready(function(){

                                  $.ajax({

                                      type: "POST", // HTTP method POST or GET

                                      url: '<portlet:actionURL name="getAlertList"/>', //Where to make Ajax calls

                                      //dataType:"text", // Data type, HTML, json etc.

                                      dataType: 'html',

                                     

                                      success: function(data) {

                                          $('#manage_user table > tbody:first').append(data);

                                          //alert(data);

                                      },

                                      error: function(xhr, ajaxOptions, thrownError) {

                                          //On error, we alert user

                                          alert(thrownError);

                                      },

                                      complete: function() {

                                          //alert('update success');

                                      }

                                  });

                                 

                              });

                          </script>

      2- Added code into portlet controller

      @ProcessAction(name="getAlertList")

          public void getAlertList(ResourceRequest request, ResourceResponse response) throws PortletException, IOException {

             

              PrintWriter writer = response.getWriter();

                     

              int id = Integer.parseInt(request.getParameter("id"));

              IRecordsJdbcDAO recordsDAO = new RecordsJdbcDAO();

              List<AlertsForm> alertList = recordsDAO.getAlerts(id);

             

              for (AlertsForm alert: alertList) {

                  writer.write("<tr>");

                  writer.write("<tb>"+alert.getLabel()+"</tb>");

                  writer.write("<tb>"+alert.getOccurrenceDate()+"</tb>");

                  writer.write("<tb>"+alert.getClosingDate()+"</tb>");

                  writer.write("</tr>");

              }

             

              writer.flush();

              writer.close();

          }

      3- In response complete HTML of first home page displayed ..

       

      Can anyone help what is the correct way to do that? If it is correct way then what i am doing wrong.. I have also tried

      @Override
        
      public void serveResource(ResourceRequest request, ResourceResponse response) throws IOException, PortletException {

      but no resolution..

       

      Thanks,

        • 1. Re: JQuery AJAX calls within portlet jsp pages
          vstorm83

          There are 2 notes:

          - you should use serveResource in this case, but in the first snipet, i see @ProcessAction

          - the request parameter "id" mandatory on server code, but client $.ajax call doesn't send it

          • 2. Re: JQuery AJAX calls within portlet jsp pages
            larryking11

            If I get .ajax() error then I would be using .done() and .fail() deferred objects with my jQuery AJAX Method code.

            $.ajax({

                url: "someurl"

            }).done(function (result, status, xhr) {

                alert(result)

            }).fail(function (xhr, status, error) {

                alert("Result: " + status + " " + error + " " + xhr.status + " " + xhr.statusText)

            });

            In your case .fail() will be called due to some problem, and you will get the alert telling about the problem. Then you can help in debugging that problem.