繁体   English   中英

jQuery Flask POST:405方法不允许

[英]jQuery Flask POST : 405 Method not allowed

我有一个表单,何时可以在数据库中添加新元素,但是它们是错误的,并且我有新页面出现此错误:

不允许的方法所请求的URL不允许使用该方法。

在控制台中:POST http:// localhost:5000 / [HTTP / 1.0 405方法不允许2ms]

我的表格:

<form method="POST" id="formsondages" name="formsondages" style="text-align:center;"> 
    <div style="text-align:center;">
        <h4 style="text-align:center;">How ? </h4>
        <input type="text" id="name" class="form-control" placeholder=" Name...">
    </div>
    <input type="submit" id="name" style="margin-top:3px;" class="btn btn-success" value="CONFIRM">
</form>

我的JS:

$(function(){ 
$('#formsondages').submit(addSondage); 
});

function addSondage(){
    var name = $('#name').val();
    $.post("http://localhost:5000/api/sondages/",{nameS: name});
    sondageActuel = name;
    console.log("It's ok"); 
}

我的服务器(烧瓶):

@app.route('/api/sondages/', methods=['POST'])
def adSondage():
    name = request.form['nameS']
    s = Sondage(name)
    addElement(s)
    return render_template(
    "sondages.html",
        title="Sondages")

我的终端

Exception happened during processing of request from ('127.0.0.1', 53759)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 295, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 321, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
    self.finish()
  File "/usr/lib/python2.7/SocketServer.py", line 710, in finish
    self.wfile.close()
  File "/usr/lib/python2.7/socket.py", line 279, in close
    self.flush()
  File "/usr/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])

错误:[Errno 32]管道损坏

127.0.0.1 - - [03/Apr/2015 17:25:36] "POST / HTTP/1.1" 405 - `

谢谢

您是否有任何特殊原因使用jQuery提交表单?

消除jQuery部分,在表单中添加一个“动作”,看看会发生什么。

您的表格现在应该是:

<form action="/api/sondages/" method="POST" id="formsondages" name="formsondages" style="text-align:center;">

编辑:如果不起作用,您可以将GET方法添加到您的路线。

@app.route('/api/sondages/', methods=['GET', 'POST'])
def adSondage():
    if request.method == 'POST':
        # Put all your POST handling code here
    else:
        return 'GET request successfully'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM