1

Sorry I have been a backend developer and my question may look dump; my apologies; I want to have a from and submit to an API endpoint which uses PUT method and based on that if the result is 200 or 400 I want to redirect to different pages; so what I have done so far:

<form class="form-signin" method="PUT" action="MY_API">
  <h2 class="form-signin-heading">Please sign in</h2>
  <input class="form-control" type="text" required name="email" placeholder="Email address">
  <input class="form-control" type="password" required name="password" placeholder="Password">
  <input class="form-control" type="text" required name="name" placeholder="name">
  <button class="btn btn-lg btn-primary btn-block" type="submit">Sign in</button>
</form>

But after submitting it goes to the url from action as GET data, why is that?!

When I submit the page to the API endpoint I want to check the HTTP status code and based on that redirect to different pages.

Also, it is worth mentioning I'm open to any solution which is the fastest(in terms of implementation) and easiest one either pure javascript, JQuery,...

Thanks

  • Fastest performance is always plain javascript. Easiest is almost always jquery (for ajax stuff). Do you have a `e.preventDefault()` on your submit event? – ODelibalta Oct 02 '15 at 16:22
  • Thanks @ODelibalta I can use Jquery; Could you please help me how I cna handle it in JQuery please? –  Oct 02 '15 at 16:24
  • Just to add that there are thousands (literally) of questions on stack overflow about how to use jQuery/Ajax, many of them specifically for PUT. – Basic Oct 02 '15 at 16:41
  • [XMLHttpRequest (MDN)](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest) is also relevant. –  Oct 02 '15 at 17:28

1 Answers1

0

The HTML form spec only allows GET and POST, any unknown methods go through GET. What you're going to need to do is create an XMLHttpRequest through javascript programmatically, and set it to PUT with a request body.

TbWill4321
  • 8,626
  • 3
  • 27
  • 25
  • Could you please help me how I can handle it through `XMLHttpRequest`? –  Oct 02 '15 at 16:25
  • @user3399784 in pure javascript, it [varies slightly between browsers](http://stackoverflow.com/questions/2557247/easiest-way-to-retrieve-cross-browser-xmlhttprequest). I'd strongly recommend using a library like jQuery which removes the cross-browser pain for you and gives you [handy helper methods](http://api.jquery.com/jquery.ajax/) – Basic Oct 02 '15 at 16:28
  • Can I use Ajax for my example and redirect to different pages after form submission? –  Oct 02 '15 at 16:35
  • You'll get a response in a callback method in JS. You can examine the HTTP status code, headers, body content, local variables, etc and take any action you wish, including changing the current url – Basic Oct 02 '15 at 16:39