3

I'm trying to process an AJAX request using catalyst to resolve some dynamic html generation trough jQuery. Simply put, I need to fill a <select> </select> tag (created through jQuery, along with some other fields, on user input). To fill this tag I thought I could extend the code block creating the tags to perform a simple AJAX request for data I need... Something like this:

 $.post("../ajax/simple_query", 'get="family_precedents"' );

And then process this very simple request on a perl script using Catalyst::Request like I normally would.

The problem is that AJAX response contains the Catalyst App wrappers and makes it really messy to both understand and use. Is there any way to avoid wrappers on an AJAX response using catalyst?

Leitouran
  • 578
  • 3
  • 16
  • 1
    By wrappers you mean your Template wrapper? Your content is a block of HTML? Is it created via a template, or do you set it directly in your controller? Please [edit] your question and include the part of the controller that renders the page, and the template. – simbabque Jan 25 '17 at 21:01
  • Yes, I meant Template Wrappers. The ajax response created content is more like a string containing various values that I parse directly in the javascript. – Leitouran Jan 25 '17 at 21:10
  • If it's not even created by your templates then you can also just use a different View. You would typically use https://metacpan.org/pod/Catalyst::View::JSON for aja[xj]. – simbabque Jan 25 '17 at 21:26
  • Yeah! I'd actually consider this option if I have to handle more AJAX requests. This was supposed to be a quick fix and I am not familiar with JSON (though I do recognize is a great tool :P). Thanks a lot for the tip :) – Leitouran Jan 26 '17 at 03:49
  • Quick fixes are the kind of thing that make your life hard later. Use the json view. Making it will take you a minute, and then you only need two lines of code in your controller. The rest will just work. You put your Perl data structure in the stash and it becomes json. In the frontend your jquery just treads it as a Javascript variable. – simbabque Jan 26 '17 at 08:03

1 Answers1

3

I found the answer digging a little bit more into catalyst documentation. Turns out you can configure the wrappers to appear conditionally, like this:

[% IF no_wrapper or template.name.match('\.(css|js|txt)');
  debug("Passing page through as text: $template.name");
  content;
 ELSE;
  debug("Applying HTML page layout wrappers to $template.name\n");
  content WRAPPER site/html + site/layout;
 END; -%]

And then send the no_wrapper value along with the $c->stash->{no_wrapper}

Source: http://wiki.catalystframework.org/wiki/gettingstarted/howtos/disabling_a_tt_wrapper_for_ajax_requests

Leitouran
  • 578
  • 3
  • 16