[Dancer-users] ajax plugin strangeness

Puneet Kishor punk.kish at gmail.com
Wed Oct 12 15:05:04 CEST 2011


On Oct 12, 2011, at 2:36 AM, Stephen Fenwick-Paul wrote:

> I've experienced something similar.
> 
> If I have a Ajaxed request that fails due to a server error, then there is a
> good chance that the page that the request was made from will, on the next
> page refresh, l also be treated as though it is an ajax page, i.e. it will
> lose its headers and footer etc.
> 
> My guess is that the ajax plug has some global variable that does not get
> reset after a failed request. Sadly, I've not found a way
> of consistently reproducing it.
> 
> I'm on apache.



Ok, good to know I was not hallucinating. Keep in mind, I was experiencing the strangeness without any server error involved. Just erratically, for exactly the same kind of request (identical headers, just different query params) submitted via the same jQuery method, I would get back a JSON stream (correctly) or a web page (incorrectly).

I have gone removed Dancer::Plugin::Ajax now, but as a result, have gone rather verbose to accomplish everything. Here is my pattern, and I seek the communities suggestions on improving it.

Assuming I want to get a resource called "foo" I am now implementing two routes (that is, two routes per resource)

	# when foo is requested as http://server/foo
	# a complete web page should be returned
	get '/foo' => sub {
		my $p1 = params->{'p1'};
		my $p2 = params->{'p2'};
		..
		template "foo", \%options;
	};
	
	# when foo is requested as an ajax call via http://server/foo.json
	# only a JSON packet should be returned
	get '/foo.json' => sub {
		my $p1 = params->{'p1'};
		my $p2 = params->{'p2'};
		..
		return to_json \%options;
	};

and, for the "index" page, that is, the page when there is no path_info, assuming foo is the default resource that should be returned, the following additional route is before the above two

	# when user goes to http://server/
	# a complete web page should be returned
	get '/' => sub {
		my $p1 = params->{'p1'};
		my $p2 = params->{'p2'};
		..
		template "foo", \%options;
	};
	
Is the above the right way or is there a better way?


> 
> On Wed, Oct 12, 2011 at 1:57 AM, Puneet Kishor <punk.kish at gmail.com> wrote:
> 
>> Perhaps I don't understand how to correctly use the Dancer::Plugin::Ajax. I
>> am running Dancer 1.3072 with Dancer::Plugin::Ajax running off of Starman
>> proxies behind Apache2. I experience the following strangeness (which, you
>> can test at http://humanesettlements.punkish.org)... Some Ajax queries
>> (sent via jQuery) return as JSON, while others return as full html in spite
>> of having identical headers. My server side code is
>> 
>>       ajax '/' => sub {
>>               my $page = params->{'page'} || 1;
>>               my $tags = params->{'tags'};
>> 
>>               my $res = query('page' => $page, 'tags' => $tags);
>> 
>>               return to_json \%res;
>>       };
>> 
>>       get '/' => sub {
>>               my $page = params->{'page'} || 1;
>>               my $tags = params->{'tags'};
>> 
>>               my $res = query('page' => $page, 'tags' => $tags);
>>               template "ideas", \%res;
>>       };
>> 
>> 
>> Here are the queries as captured in the web debugger (in Safari). The
>> following returned JSON
>> 
>>       • Request URL:http://humanesettlements.punkish.org/?tags=old_people
>>       • Request Method:GET
>>       • Status Code: 200 OK
>>       • Request Headers
>>               • Accept:application/json, text/javascript, */*; q=0.01
>>               • Referer:http://humanesettlements.punkish.org/
>>               • User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1)
>> AppleWebKit/534.48.3
>>                    (KHTML, like Gecko) Version/5.1 Safari/534.48.3
>>               • X-Requested-With:XMLHttpRequest
>>       • Query String Parameters
>>               • tags:old_people
>>       • Response Headers
>>               • Connection:Keep-Alive
>>               • Content-Length:2905
>>               • Content-Type:text/xml; charset=utf-8
>>               • Date:Wed, 12 Oct 2011 00:42:29 GMT
>>               • Keep-Alive:timeout=5, max=99
>>               • Server:Perl Dancer 1.3072
>>               • X-Powered-By:Perl Dancer 1.3072
>> 
>>       JSON response (snipped): {"prev_str":"..}
>> 
>> The following failed in that it returned full HTML content in spite of
>> sending the correct revue headers
>> 
>>       • Request URL:
>> http://humanesettlements.punkish.org/?tags=old_people+implementation
>>       • Request Method:GET
>>       • Status Code: 200 OK
>>       • Request Headersview source
>>               • Accept:application/json, text/javascript, */*; q=0.01
>>               • Referer:
>> http://humanesettlements.punkish.org/?tags=old_people
>>               • User-Agent:Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_1)
>> AppleWebKit/534.48.3
>>                    (KHTML, like Gecko) Version/5.1 Safari/534.48.3
>>               • X-Requested-With:XMLHttpRequest
>>       • Query String Parametersview URL encoded
>>               • tags:old_people implementation
>>       • Response Headersview source
>>               • Connection:Keep-Alive
>>               • Content-Length:21176
>>               • Content-Type:text/xml; charset=utf-8
>>               • Date:Wed, 12 Oct 2011 00:42:37 GMT
>>               • Keep-Alive:timeout=5, max=100
>>               • Server:Perl Dancer 1.3072
>>               • X-Powered-By:Perl Dancer 1.3072
>> 
>>       HTML response (snipped): <!doctype html>
>> 
>>               <!--
>>               hacked by Puneet Kishor, based on HTML Boilerplate by Paul
>> Irish and Divya Manian
>>               all modifications released under a CC0 waiver by Puneet
>> Kishor
>>               March 15, 2011
>> 
>>               See http://html5boilerplate.com/ for the original
>>               -->
>> 



More information about the Dancer-users mailing list