2

I am using an ajax function to verify user login and i am returning json on errors . i wanted to redirect to a particular url on successive login, but the i can only send json data from my function (eliminating the possibility of using url_for or redirect ) . so How do i dynamically get the root url so i can send it via json and then redirect via javascript. heres my route`

def logincheck():
    uname = request.form['username']
    pwd = request.form['password']
    if uname and pwd:
        this = userlogin.query.filter_by(username = uname).first()
        if this:
            if this.password == pwd:
                session['myid'] = this.uid
                return jsonify(success = ?)
            else:
                return jsonify(p_error = 'Incorrect Password')
        else:
            return jsonify(u_error = 'Incorrect Username')

Thanks.

3
  • So i ended up using location.reload() to refresh the page, since the user had been logged in. a refresh automatically redirected to the desired page/ Commented Aug 27, 2017 at 15:17
  • Something like this $.ajax({ type: "POST", url: pathname, data: data, success: function(data) { if(data.redirect) { window.location = data.redirectURL; } } in your ajax call and pass the redirectURL through flask. In flask return jsonify(data=data) where data contains redirectURL etc Commented Aug 27, 2017 at 15:23
  • the problem would still have been getting the root url.. since i am testing this on localhost the url would be different than the actuall production server Commented Aug 28, 2017 at 4:00

1 Answer 1

1

To get base url in javascript ,

var base_url = window.location.origin;

output : "http://yoururl.com"

var host = window.location.host;

output : yoururl.com

To redirect url in javascript ,

// redirect to another page

`window.location = "http://www.yoururl.com"`;

// it has similar behavior as an HTTP redirect

window.location.replace("http://yoururl.com");

// it has similar behavior as clicking on a link

window.location.href = "http://yoururl.com";

Hope this will help you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.