Search code examples
jqueryajaxmicronaut

Calling Micronaut service using AJAX fails


I have a Micronaut service that is invoked from a client using an AJAX call implemented in JQuery. The Micronaut service is shown below:

 @Controller("/ajaxcaller")
 public class AjaxService
{
   @Get("/printit")
   @Produces(MediaType.TEXT_PLAIN)
   public String updateData(Session theSess)
   {
      System.out.println("Accessing from AJAX.");

      return("Successful!");
   }
}

The Javascript that invokes the micronaut service is below:

  $.ajax({
      url : '/ajaxcaller/printit',
      type: "GET",
      contentType: 'application/text',
      success: function(data, textStatus, jqXHR)
      {
         alert("Get successful! "+data);
      },
      error: function (jqXHR, textStatus, errorThrown)
      {
         alert("Get failed! Error: "+jqXHR.status+", "+errorThrown);
      }
  });

Unfortunately, despite the fact that I am telling the server and the client to handle plain text, the AJAX call fails with the following message:

 Get failed! Error: 415, Unsupported Media Type

Have I found a bug in Micronaut's controller handlers, or am I missing something in either my client or server code? Is there some way to get this AJAX call to work?

UPDATE: following the advice provided by James in answer 1 below, the following changes were made to the code as shown below:

 @Controller("/ajaxcaller")
 public class AjaxService
 {
   @Get("/printit")
   @Consumes("application/text")
   public String updateData(Session theSess)
   {
      System.out.println("Accessing from AJAX.");

      return("Successful!");
   }
}

I am still getting the 415 failure message when I attempt to use the service.

Because I have created and successfully used other Get services in Micronaut before, I performed a test on this one using a different page. Basically, instead of using an ajax call to invoke the service, I used the following line in the HTML file:

window.location.href = '/ajaxcaller/printit';

Calling this causes a new page to be generated that displays the string returned by the service (which was, of course, the expected behavior). No 415 errors are generated -- even if I remove the @Consumes annotation.

Clearly, the Micronaut service is handling the AJAX invocation differently from a regular page invocation. Is there a setting on the AJAX side that I am missing, or is there a bug in Micronaut that is preventing successful AJAX invocations?


Solution

  • It turns out that Rohit Mittal's comment identified the problem. By removing the contentType setting from the AJAX call, I was able to eliminate the 415 failures.

    I did some further experimentation, and discovered that with AJAX calls the content type should be specified accurately both at the client and the server. If, however, you are just doing a GET using AJAX without passing anything to the service, then do not set a content type, because that will cause 415 failures whether you add a @Consumes annotation to the service or not.

    If you are not using AJAX, these considerations apparently become irrelevant. Micronaut services work under these conditions when AJAX calls fail.