POST request redirected to GET in submit form

I use parse.com with their expressjs framework. I have this html form that calls my / login url with a message, but for some reason it is redirected (with a status code of 301) to a get request to this URL.

This is my html form

<html> <head></head> <body> <form method="post" action="/login"> <label>Username</label> <input name="username"></input> <label>Password</label> <input name="password" type="password"></input> <input class="button" type="submit" value="Log In"> </form> </body> 

To make the question more clear, I use express js from parse.com, and here are two specific routings

 app.get('/login', function(req, res) { res.send('get is called'); }); app.post('/login', function(req, res) { res.send('post is called'); }); 

Now, no matter what I provide in my form method, I always get "get called" in the browser when I submit the button.

I also tried to debug what happens in the developer console, and this is what I get

enter image description here

0
source share
1 answer

I think you are using the ejs template, try adding all the attributes in your form, as without double quotes, something like this and try

 <html> <head></head> <body> <form method=post action=/login> <label>Username</label> <input name="username"></input> <label>Password</label> <input name="password" type="password"></input> <input class="button" type="submit" value="Log In"> </form> </body> </html> 

I checked in the console, and the form attributes when double quotes were added were something like this

 <form action=""/login"" method=""POST""> 

therefore, by default, the form is submitted as a GET request instead of a POST. I'm still not sure why this is happening, I just started to study the express, will add more details when I receive them.

0
source

All Articles