2

I have an application that uses Sammy for some simple client-side routing.

One of the pages has a "Download Pdf" button, which needs to do a POST to get and download a pdf document (not very resty, I know, but it has to be a POST due to the large amount of data I'm submitting). It does this using the old trick of dynamically creating, populating, and submitting a <form> element.

Everything works fine, except for I can see in the console an error from sammy that my route was not found. Note that this is not a route, or even a verb that Sammy should be handling.

Here is my reduced test case

Sammy(function initializeClientRouting(app) {
  app.get('#/', show('#default'));
  app.get('#/test', show('#test'));

  function show(selector) { return function() {
    $('section').slideUp();
    $(selector).slideDown();
  }; }
}).run('#/');

$('button').click(function() {
  var form = $("<form method=post action: 'http://www.google.com'>").hide();
  $('<textarea name=q>').text("search text").appendTo(form);
  form.appendTo('body').submit().remove();
});

Does anyone know how to prevent this error? Is this a bug in Sammy?

George Mauer
  • 117,483
  • 131
  • 382
  • 612

1 Answers1

1

It's a combination of sammy & JQuery behaviour (bug?). When generated dynamically the way you put it, the form tag is being rendered as

 <form www.google.com'="" 'http:="" action:="" method="post">

This will try to POST to the current page which probably is something like

http://blah/# or http://blah/#/test

For some reason, Sammy will be triggered because of the hashtag, not finding a POST configured and log an error.

Fiddling with your example, what worked for me was:

    var form = $("<form>");
    form.attr('method', 'post');
    form.attr('action', 'http://www.google.com');
    $('<textarea name=q>').text("search text").appendTo(form);
    form.appendTo('body').submit().remove();

This seemed to generate the proper HTML and remove the Sammy error.

rivarolle
  • 1,528
  • 11
  • 18
  • Sorry, but [your code](http://jsbin.com/ubuser/7/edit) does not remove the sammy error. Moreover [here is a cleaned up version](http://jsbin.com/ubuser/6/edit) with definitely correct html being generated (requires chrome). Still an error. The error definitely seems to lie within sammy – George Mauer May 06 '13 at 16:53