jQuery Flask POST:405方法不允许

时间:2015-04-03 15:33:50

标签: jquery python ajax post flask-sqlalchemy

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

不允许的方法 请求的网址不允许使用此方法。

在控制台中:POST http://localhost:5000/ [HTTP / 1.0 405 METHOD NOT ALLOWED 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"); 
}

我的服务器(Flask):

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

谢谢

1 个答案:

答案 0 :(得分:0)

您是否有任何特殊原因要使用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'