POST请求在表单提交时被重定向到GET [英] POST request being redirected to GET on form submit

查看:136
本文介绍了POST请求在表单提交时被重定向到GET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将parse.com与他们的expressjs框架一起使用.我有这个html表单,该表单用post调用我的/login url,但是由于某种原因,它被重定向(状态码为301)到对该URL的get请求.

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

这是我的html表单

<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>

为了使问题更清楚,我在parse.com中使用express js,这是定义的两个路由

To make the question more clear i use express js with parse.com and here are the two routing defined

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 is called" in the browser on submitting the button.

我还尝试调试开发者控制台正在发生的事情,这就是我所得到的

I also tried to debug what is happening in by the developer console and this is what i get

推荐答案

我认为您正在使用ejs模板,尝试将表单中的所有属性添加为不带双引号的方法,然后尝试

I think you are using 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 was something of this sort

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

因此,默认情况下,表单是作为GET请求而不是POST提交的.我仍然不确定为什么会发生这种情况,刚开始学习Express时,会在我得到这些细节时添加更多细节.

so by default the form was getting submitted as a GET request instead of a POST. I am still not sure why this is happening, just started learning express, will add more details when I get them.

这篇关于POST请求在表单提交时被重定向到GET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆