I used ajax call to send data to server from a form and receive data as a jsonify. But I'm getting Error that "The method is not allowed for the requested URL." and I cannot what is wrong with the code. This is form data.
<form class="form-submit" method="post" role="form">
<div class="input-group">
<input name="userID" id="userID" type="text" class="form-control width100" placeholder="Input User ID" style="width: 80%;">
<span class="input-group-btn">
<button id="button" class="btn btn-secondary" type="submit"><i class="fa fa-search"></i></button>
</span>
</div>
</form>
Following is ajax call to post data. Here I'm calling from post.html
.
$('#button').click(function (event) {
$.ajax({
data:{
userID : $('#userID').val()
},
type : "POST",
url : '/charts'
}).done(function (data) {
console.log(data.id);
});
event.preventDefault();
});
And Here is my relevant code.
@app.route('/posts',methods=["POST"])
def posts():
if request.method == "POST":
id = request.form['userID']
return jsonify({"id": id})
return render_template('posts.html')
I cannot figure out what is the problem with this. For this I'm getting both errors which are,
POST http://127.0.0.1:5000/posts 405 (METHOD NOT ALLOWED)
and
GET http://127.0.0.1:5000/favicon.ico 404 (NOT FOUND)
Can anyone help me with this. I checked all the relevant questions and didn't found a help.