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!
alert
before thewindow.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.ajax
call did not run in the first method. After removingfrm.submit()
Approaches 1 and 2 generate the same 405 errors. Any suggestions?type
toget
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.frm.submit()
which created a post request and it worked...