0

I am using ajax to submit a html input form and to redirect the output page when it is done. I tried two approaches but not sure why their results are different.

HTML form is something looks like this:

<form id="output_post" method="post" action="output.html">
    <table class="input"></table>
</form>

Approach 1:

    var frm = $('#output_post');
    frm.submit()
    $.ajax({
        type: frm.attr('method'),
        url: frm.attr('action'),
        success: function (url) {
            window.location = "/output.html"
        }
    });

Approach 2:

    var frm = $('#output_post');
    $.ajax({
        type: "POST",
        url: frm.attr('action'),
        success: function(url) {
            window.location = "/output.html"
        }

    });

Approach 1 worked as I expected but I got error message in Approach 2 405 Method Not Allowed The method GET is not allowed for this resource. The difference between Approaches 1 and 2 is frm.submit(), and I am very sure both approaches have successfully initiate calculation.

Could anyone give me some hints on this issue? Thanks!

7
  • Does your AJAX call actually run in the first approach? You can test by adding an alert before the window.location line. I believe submitting a form should stop execution of all JS on the page as you're sending a request for another page.
    – Jasper
    Commented Oct 14, 2013 at 21:23
  • @Jasper: Yes you are right. It seems like ajax call did not run in the first method. After removing frm.submit() Approaches 1 and 2 generate the same 405 errors. Any suggestions?
    – TTT
    Commented Oct 14, 2013 at 21:25
  • Your server apparently isn't setup to handle POST requests. Try changing the type to get and see if you get the same error. If you don't get the same error, look into how to setup your server software (technology stack) to properly handle POST requests.
    – Jasper
    Commented Oct 14, 2013 at 21:27
  • @Jasper I think the server can handle POST. Because when I included frm.submit() which created a post request and it worked...
    – TTT
    Commented Oct 14, 2013 at 21:28
  • Are you sure you're not getting an error when the form submits normally (without AJAX)? In Chrome you can watch your network tab to see what exactly is being returned as a response from the server after submitting the form. See if you're getting a 405 response code there.
    – Jasper
    Commented Oct 14, 2013 at 21:34

2 Answers 2

2

Firstly, I would actually say .submit() would be better reserved for allowing the Browser to actually go ahead with the natural/indented behaviour of following through to the action="" - if you wanted to actually have a different 'end result' - that's where $.submit() comes into help.

/**
 *   $.submit will listen to
 *   the .submit event, preventing
 *   default and allowing us to mimic
 *   the submission process and have our
 *   bespoke end result.
**/
$('#output_post').submit(function(event) {
    event.preventDefault();
    var getValues = $(this).serialize();
    $.post( 'yourScript.php', getValues, function() {
        window.location.href = "/output.html";
    } 
});

Comments to the Question

Approach One

  • This 'leaves' the function. Prematurely sending you away from the page before it's allowed to execute the rest of the script. The native .submit() will follow through to the action, which is the indented behaviour. Thus, $.ajax never ran.

Approach Two

  • Servers could/can decide on it's accepted content types.
  • No Data was sent to the URL, thus - defaulted to GET (Despite type: POST) But there's no serialised. array given.
  • This may 'act' different if you define 'data: values' in Approach Two.
1
  • Thanks so much for the detailed explain. I doubled checked my code and it seems like the issue (maybe) related in the selection of dataType. If possible, would you mind taking a look at this post?stackoverflow.com/questions/19372808/…
    – TTT
    Commented Oct 15, 2013 at 3:28
0

Try this:

var frm = $('#output_post');

$('#output_post').submit(function(event) {

$.ajax({
        type: "POST",
        url: frm.attr('action'),
        success: function(url) {
            window.location = "/output.html"
        }

    });

});

- Thanks

2
  • Thanks for your reply. But I still get the same issue.
    – TTT
    Commented Oct 21, 2013 at 20:53
  • I checked the log and it seems like the console.log(url) has my output page. But the redirection breaks it.
    – TTT
    Commented Oct 22, 2013 at 15:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.