AJAX request can't read updated session/global variable
<p>I am using Flask with google-python-api-client to get data from Youtube.</p>

<p>I am trying to load asynchronously next video from the playlist when a button is pressed. For that I need to set the <code>nextPageToken</code> parameter when making the request</p>

<pre><code>    $(document).ready(function() {
        $('#more_content').click(function() {
            $.ajax({
                type: "GET",
                url: "/test",
                data: {np_token: '{{ g.np_token }}'},
                success: function(result) {
                    $("#content").append(result);
                }
            });
        });
    });
</code></pre>

<p>The function I am using to get the next video:</p>

<pre><code>def get_another_video():
    res = YOUTUBE.playlistItems().list(
        part="snippet",
        playlistId=PLAYLIST_ID,
        maxResults="1",
        pageToken=request.args.get('np_token', None)
    ).execute()
    g.np_token = res.get('nextPageToken', None)
    return json.dumps(res)
</code></pre>

<p>This is not working because AJAX request cannot read the updated value from the global variable <code>g.np_token</code>, and the same result will be if I'll set a session variable.</p>

<p>How do I solve this problem? The passed parameters to AJAX will be available only in the case of a <code>success</code> request, but I need them to be accessible in the <code>data</code> part.</p>

<p>I've read that for this kind of task, redis or memcache might be suitable, but I don't know if I'll be able to access those variables in the templates.
Thanks!</p>