0

I am using the post method for the login. My ajax function sends the data successfully to my flask backend server [I know because it returns a response to my ajax]. Supposedly, after receiving the respnse from the backend, my ajax success handler will navigate/redirect to the dashboard page but IT DOES NOT! How do I make it navigate/redirect to another page/url?It returns a 200 status code so I do not know why it does not display the dashboard page.

WHAT I HAVE TRIED:

I have tried using window.location.href, window.location.replace but to no avail, still it does not work. I have also tried changing the method to GET but its still the same. I have also set async to false because ajax would not post if I would not set it to false.

AJAX

$.ajax({
    type: "POST",
    url: 'http://127.0.0.1:5000/processlogin',
    data: JSON.stringify(loginobject),
    contentType: "application/json;charset=utf-8",
    async: false,
    success: function (resp) {
        window.location.href = ("http://127.0.0.1:5000/dashboard");
    },//success
    failure: function (resp) {
        alert(resp.message);
    }

});

backend flask functions This functions work 100%. Already tested it with POSTMAN. I have also queried the database using my stored procedure and it does well.

This displays the login form

@app.route('/', methods=['GET','POST'])
def login():
    return render_template('login.html')

This processes the ajax's sent data. In short this is the function ajax is communicating with

@app.route('/processlogin', methods=['POST'])
def processlogin():
    loginobject = request.get_json(force=True)
    username = loginobject['username']
    password = loginobject['password']

    try:
        dbpassword = callstoredproc("getpassword", (username,))[0][0]
        if dbpassword == 'null':
            return jsonify({'status':'error', 'message':'Username does not exist!'})
        elif bcrypt.verify(password, dbpassword) == True:
            return jsonify({'status':'ok'})
    except Exception as e:
        print(e)

And this is what I am trying to display: the dashboard html

@app.route('/dashboard', methods=['GET', 'POST'])
def dashboard():
    return render_template('dashboard.html')
Mantis
  • 1
  • 4
  • Put an alert()/console.log() call to the "success" callback and look if it's shown to be sure you are coming back correctly – Michael Jun 27 '18 at 14:19
  • @Michael I already tried that... when I entered a wrong password, it goes to the eerror handler but when I enter the correct ones, instead of navigating to the page I specified in the success handler, it alerts 'undefined' but in the console, it returns a status code of 200 – Mantis Jun 28 '18 at 02:06
  • Try to simplify your use case. For example replace the ajax call to your backend to a dummy rest api (like I did in the jsFiddle) and look if it's working. If this works you know that the response from your backend is somehow "broken" and invalid. – Michael Jun 28 '18 at 08:20

1 Answers1

0

Remove the curved brackets and try again:

window.location.href = "http://127.0.0.1:5000/dashboard";

It works also with curved brackets so just be sure that your response arrive correctly to the success callback.

See also best answer on SO.

It should also be error instead of failure as error callback.

error: function (resp) {
    alert(resp.message);
}

jsfiddle Example

Michael
  • 189
  • 1
  • 17