javascript - Flask method not allowed 500 -
i'm working on sign in form , receiving following message:
method not allowed method not allowed requested url. reading number of questions suggests uri of flask handler should same 1 specified in form's action attribute. i'm trying submit form's fields part of json object using ajax api jquery:
form.html
<form id="signin_form_id" onsubmit="sign_in()" method="post"> <label>email: </label><input id="email0" type="email" name="l_email" required> <br> <label>password: </label><input id="password0" type="password" name="l_password" required> <br><br> <input type="submit" value="submit"> </form> <!-- body of index page --> <body> <div class id="main"></div> <script> <!-- display index page if session token not set --> {% if page_body %} if (localstorage.getitem("token") === null) { document.getelementbyid("main").innerhtml = {{ page_body|safe }} } {% endif %} </script> </body> the function sign_in() defined bellow:
client.js
function sign_in() { var uri, method, formid, jsonobject; uri = location.protocol + '//' + location.host + "/sign_in"; method = "post"; formid = "#signin_form_id"; // set-up ajax call var request = { url: uri, type: method, contenttype: "application/json", accepts: "application/json", cache: false, datatype: 'json', data: json.stringify($(formid).serializearray()) }; // make request $.ajax(request).done(function(data) { // handle response if(data.successsignin === false) { // login failed; display index page alert("login failed!"); document.getelementbyid("main").innerhtml = document.getelementbyid("welcomeview").innerhtml; } else { // login succeeded. load user's info, messages , form in can type messages // save token received server. stored cookie localstorage.setitem('token', data.token); // go home page go_home(); } }).fail(function(jqxhr) { // handle failure console.log("ajax error upon sign in " + jqxhr.status); } ); location.reload(); } the server side code handle's request is:
serverside.py
@app.errorhandler(400) def page_not_found(e): # page returned if request not contain json object page_body = 'document.getelementbyid(\"welcomeview\").innerhtml;' return render_template('client.html', page_body=page_body) @app.route('/sign_in', methods=['post']) def sign_in_helper(): json_obj, code = decorator(sign_in, request, check_token=false) # check if credentials valid if code == 401: # invalid login page_body = 'document.getelementbyid(\"welcomeview\").innerhtml; alert(\"invalid credentials!\")' return render_template('client.html', page_body=page_body) else: # return token, operation completion flag , response code return json_obj, code def sign_in(email, password): data = query_db('select * users email = ?', [email], one=true) if data , check_password_hash(data["password"], password): token = token_creator() insert_db('update users set token = ? email = ?', [token, email]) return jsonify( successsignin=true, message="welcome", data=json.dumps({'token': token}), ), 200 return jsonify( successsignin=false, message="username or password invalid"), 401 def decorator(func, request, check_token): data = request.get_json(force=true) try: if check_token: token = data.get('token', none) if token: user = query_db('select * users token = ?', [token], one=true) if user: json_obj, code = func(**data) else: json_obj = jsonify(success=false, message='invalid token') code = 401 else: json_obj = jsonify(success=false, message='misformatted data.') code = 400 else: json_obj, code = func(**data) except (keyerror, typeerror, valueerror): json_obj = jsonify(success=false, message='misformatted data.') code = 400 return json_obj, code if set action attribute of form /sign_in error no longer sent, there 2 submissions of form data: 1 form , 1 in ajax call.
why isn't inner html set accordingly within jquery call?
Comments
Post a Comment