Reputation: 19329
With two <a>
:
<a id="100" onclick="createTriggered(this.id)"<i></i> Click Link </a>
<a id="200" onclick="createTriggered(this.id)"<i></i> Click Link </a>
both linked to the same onClick
javascript function:
<script type=text/javascript>
$SCRIPT_ROOT = {{ request.script_root|tojson|safe }};
function onClick(id){
var data = $.getJSON($SCRIPT_ROOT + '/triggered', {a:id} );
window.alert({{ "session.keys()" }});
}
</script>
This javascript function takes the clicked <a>
id and sends it to flask function:
@app.route('/onClick')
def onClick():
session['a_id'] = request.args.get('a')
return ('ok')
After Flask function gets id
it updates session
object creating a new key session['a_id']
.
Finally javascript window.alert
is used to check if the session
object has the new 'a_id' key which should be already set by Flask function. When window.alert
pops up it shows that the new key is missing. Refreshing the page and re-clicking the <a>
link will pop up the window.alert()
again but this time showing the new session['a_id']
key is there. It looks like the update of the session
object is lagging behind. I have to refresh the page in order for the browser to pick up the change made by Flask function. Is there a way to assure that the update made by the Flask function to the session
object is reflected immediately?
Finally, since I am updating the session
object which is global to the entire session I really don't want to return any template or to redirect to another link. All I want is Flask function to set the session
variables so it could be used somewhere down the logic. Would it be ok to return a dummy string from Flask function in this case?
Upvotes: 0
Views: 1677
Reputation: 3457
It looks like the update of the session object is lagging behind.
It is not lagging. The issue is that originally you don't have anything in your session
, thus your HTML page and your javascript is equivalent to
window.alert()
.
After you update your session
, re-render the page update the javascript code to window.alert('a_id')
.
Is there a way to assure that the update made by the Flask function to the session object is reflected immediately?
You can examine your javascript callback to check the cookie return. The cookie is encrypted, so you won't see the value unless you know how to decrypt it. It is not recommended to do that. However, at the very least you can see the value of the cookie is changed.
Upvotes: 1