1
votes

Trying to use REST services with Catalyst.

I can get a response sent back with JSON, however, how do I do so if I wanted an HTML template to be rendered. I have:

    package MM::Controller::User::Test;
    use Moose;
    use namespace::autoclean;

   BEGIN { extends 'Catalyst::Controller::REST' }

   sub thing : Local : ActionClass('REST') { }

   sub thing_GET {
      my ( $self, $c ) = @_;

      # Return a 200 OK, with the data in entity
      # serialized in the body
      $c->{stash}->{template} = "a.tt";

      $self->status_ok(
           $c,
           entity => {
               some => 'data',
               foo  => 'is real bar-y',
           },
      );
   }


   1;

And my jQuery:

   $(document).ready(function() {

     $.ajax({
        type    : "GET",
        url     : "/user/test/thing",
        data    : {},
        dataType: "html",
        success : function(data, textStatus) {
          console.log(data);
        }
      });
    });

It works if dataType is "json" perfectly (Object {foo: "is real bar-y", some: "data"} ), however, how do I get Catalyst to return some HTML so jQuery can place it. I obtain a "415 (Unsupported Media Type) " message. Am I just not using the REST properly?

1

1 Answers

0
votes

If all you want to return is some HTML ready to be injected into your page, you may be over-complicating it by attempting to use REST as well.

I would try configuring this as a regular action BEGIN { extends 'Catalyst::Controller' } and see what you get by just handing off to the template and removing the $self->status_ok() call.

That is certainly how I have built Catalyst AJAX calls that return template output.