Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I'm just starting out learning how to use the Dancer2 framework and I'm having problems with figuring out how to configure the route to display the returned data with or without the use of the Dancer2::Plugin::Ajax module.

For my initial test the route simply returns "ajax test" but the goal is to return the results of a DB query.

package Templar; use DBI; use Dancer2; #use Dancer2::Plugin::Ajax; use Dancer2::Plugin::Database; use Template; use HTML::Escape qw/escape_html/; use Crypt::SaltedHash; use Data::Dumper; our $VERSION = '0.1'; hook before => sub { if (!session('logged_in') && request->dispatch_path !~ m{^/login}) + { return forward '/login', { requested_path => request->dispatch +_path }; } }; #ajax '/p_details' => sub { "ajax test" }; get '/p_details' => sub { "ajax test" };

If I use the ajax route, I get a "404 Not Found" error for the p_details page/route.

The p_details.tt template is:

<script> $(document).ready(function() { $("#folder").find("[id^='tab']").hide(); // Hide all content $("#tabs li:first").attr("id","current"); // Activate the first ta +b $("#folder #tab1").fadeIn(); // Show first tab's content $('#tabs a').click(function(e) { e.preventDefault(); if ($(this).closest("li").attr("id") == "current"){ //detectio +n for current tab return; } else{ $("#folder").find("[id^='tab']").hide(); // Hide all content $("#tabs li").attr("id",""); //Reset id's $("#tabs li").removeAttr("id"); $(this).parent().attr("id","current"); // Activate this $('#' + $(this).attr('name')).fadeIn(); // Show content for +the current tab } }); partner_details('general'); }); </script> <span id="top"> <h2>Partner Details</h2> <h3>The partner details function is still in development and is not ye +t ready to be used.</h3> </span> <ul id="tabs"> <li><a href="#" onclick="partner_details('general')">General</a></li +> <li><a href="#" onclick="partner_details('qualifier')">EDI Qualifier + / ID</a></li> <li><a href="#" onclick="partner_details('certificates')">Certificat +es</a></li> <li><a href="#" onclick="partner_details('contact')">Contact Informa +tion</a></li> <li><a href="#" onclick="partner_details('notes')">Notes</a></li> </ul> <div id="folder"> </div>

The javascript is:

function partner_details(catagory) { if (! xmlHttpObj) { init_ajax(); } if (xmlHttpObj) { var url = '/p_details'; xmlHttpObj.open('GET', url, true); xmlHttpObj.onreadystatechange = partner_tab(catagory); xmlHttpObj.send(null); } } function partner_tab(tab) { var details = { general: "tab1", qualifier: "tab2", certificates: "tab3", contact: "tab4", notes: "tab5" }; //alert(details[tab]); // status is always 0 //alert('response status:' + xmlHttpObj.status); // xmlHttpObj.responseText is always empty //document.getElementById('folder').innerHTML = xmlHttpObj.respons +eText; // this updates the div as expected //document.getElementById('folder').innerHTML = "<table><tr><td>" ++ details[tab] + "</td></tr></table>"; if (xmlHttpObj.readyState == 4 && xmlHttpObj.status == 200) { // we never reach this point alert(details[tab]); document.getElementById('folder').innerHTML = xmlHttpObj.respo +nseText; //document.getElementById('folder').innerHTML="<table><tr><td> +" + details[tab] + "</td></tr></table>"; } }

Firebug shows that the status is "200 ok", Content-Type is "text/html", Content-Length is 9, and both Response and HTML show the expected "ajax test", but javascript readystate and status never change so the updating of the div or the alert window never take place.

My first thought was that I had an error in the javascript, but it works correctly in a php app. So, that means that I'm doing something wrong in the route but the Dancer2 documentation in the ajax/jquery area is almost non existent so I haven't been able to figure out the proper syntax.


In reply to How to make ajax requests in Dancer2 by fishmonger

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-23 07:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found