]> sipb.mit.edu Git - ikiwiki.git/commitdiff
improve detection of ajax request
authorJoey Hess <joey@kitenet.net>
Fri, 17 Jun 2011 19:17:20 +0000 (15:17 -0400)
committerJoey Hess <joey@kitenet.net>
Fri, 17 Jun 2011 19:17:20 +0000 (15:17 -0400)
Firefox sent an accept header for application/xml, not application/json,
and also weakened the priority to 0.8. So that stuff is not to be trusted;
instead I found a better way: When an ajax upload is *not* being made,
the Upload Attachment button will be used, so enable ajax if an upload
is being made without that button having been used.

Also, testing with firefox revealed it refused to process a response that
was type application/json, and checking the demo page for the jquery file
upload plugin, it actually returns the json with type text/html. Ugh.
Followed suite.

Now tested with: chromium, chromium (w/o js), firefox, firefox (w/o js),
and w3m.

IkiWiki/Plugin/attachment.pm

index 490014a7e870e0ff00981cf1c81b5c1f59f10aec..4d6dee23e75a2fe4e1f76889b53b19e68483c001 100644 (file)
@@ -228,7 +228,7 @@ sub attachment_store {
                check_canattach($session, $final_filename, $tempfile);
        };
        if ($@) {
-               json_response($q, $dest."/".$filename, $@);
+               json_response($q, $form, $dest."/".$filename, $@);
                error $@;
        }
 
@@ -259,7 +259,7 @@ sub attachment_store {
                });
        }
 
-       json_response($q, $dest."/".$filename, stored_msg());
+       json_response($q, $form, $dest."/".$filename, stored_msg());
 }
 
 # Save all stored attachments for a page.
@@ -360,17 +360,17 @@ sub stored_msg {
        gettext("just uploaded");
 }
 
-sub json_response ($$$) {
+sub json_response ($$$$) {
        my $q=shift;
+       my $form=shift;
        my $filename=shift;
        my $stored_msg=shift;
 
-       # for the jquery file upload widget
-       if ($q->Accept("application/json") >= 1.0 &&
-           grep { /application\/json/i } $q->Accept) {
+       if (! defined $form->submitted ||
+           $form->submitted ne "Upload Attachment") {
                eval q{use JSON};
                error $@ if $@;
-               print "Content-type: application/json\n\n";
+               print "Content-type: text/html\n\n";
                my $size=-s $filename;
                print to_json([
                        {