1

You supply the action and method properties on a form, and then when it's submitted, it completes that HTTP method using the url specified in action. The end result takes the user to a new page, the page returned by the server in response to a POST (or whatever) to that target URL.

What causes this redirect? Is it an intrinsic property of how HTML form elements work, or is it just a redirect header in the HTTP response?

I'm trying to reproduce that effect with an ajax POST, without just manually writing a line of JS to do the redirect~ I want it to happen automatically, the same way.

Is submitting a form essentially the same as going to the url www.myHostDomain.com/mySpecifiedFormAction?myFirstFormField=myFirstFormValue&mySecondFormField=mySecondFormValue ?

That can't be right, since the browser bar is always a GET request.

temporary_user_name
  • 30,801
  • 41
  • 120
  • 186
  • Which redirect are you talking about? ... The difference between a POST and a GET is that the POST can have data both in its URL and in its body, a GET has it in the URL – Ason Jul 08 '16 at 19:07
  • @LGSon - I believe OP want's to know why the server can't redirect from an AJAX request – tymeJV Jul 08 '16 at 19:09
  • Generally, the redirect will be handled server-side. What is on the back-end actually handling the submitted data? – J. Titus Jul 08 '16 at 19:09
  • When you submit an HTML form, you are taken to a new page: the browser actually renders the response from the server. What *causes* that to happen automatically? If anything the answer would be in the HTML form spec I suppose. – temporary_user_name Jul 08 '16 at 19:12
  • A form submission is like an anchor tag with an href. It pretty much performs a document.location to the new location using a get request. – scrappedcola Jul 08 '16 at 19:13
  • But if you send a GET it also return a new page? ... So does POST using the action parameter as the URL – Ason Jul 08 '16 at 19:13
  • You gotta back that up with a reference though, @scrappedcola . – temporary_user_name Jul 08 '16 at 19:13
  • In this case I think one could say, to make it simple, a POST is a GET with embedded data – Ason Jul 08 '16 at 19:15
  • 2
    If you have a look here you'll see how it works/looks: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Sending_and_retrieving_form_data – Ason Jul 08 '16 at 19:16
  • http://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit – ldg Jul 12 '16 at 17:19

1 Answers1

4

Form's action attribute serves a role of navigation URL, a.k.a. "plan to navigate". target attribute defines where to load that response returned by the server.

So yes, that is "an intrinsic property of how HTML form elements work".

Algorithm of handling form submission is defined in HTML4 and HTML5 forms specifications.

Submitting the form is essentially an ordinary URL navigation (by <a href> elements) but with automatic parameters gathering that go either into final navigation URL as parameters ( <form method="get"> ) or as request body parameters ( <form method="post"> )

c-smile
  • 24,546
  • 7
  • 54
  • 79