weixin_33711647 2011-07-16 16:44 采纳率: 0%
浏览 12

HTML + AJAX + Python +会话?

I am trying to understand how to use AJAX with Python Sessions. I understand the basics of how sessions work. When I add AJAX to the mix, my mind is having a difficult time understanding the technical details. Part of me thinks AJAX and Python Sessoins are not possible. I know web frameworks exist that probably do all this magic but I want to learn how to do it myself before jumping into a framework.

I am trying to create a webpage with a login form using HTML, AJAX, Python, and Sessions. If the user is able to log in, a session should be created (I assume that's correct). Otherwise, an error message should be returned.

Here is the basic application layout:

  • login.html : HTML form with username & password input boxes and submit button
  • ajax.js : contains AJAX function that communicates with server-side script
  • check_user.py : checks if username & password are correct, creates session or returns error
  • welcome.html : only accessible if username & password are correct
  • welcome_1.html : only accessible if username & password are correct

I prefer to keep the HTML, Javascript, and Python code in separate files as opposed to creating HTML with Python or using inline Javascript in HTML.

Here is the application logic:

user visits login.html
    enters username & password
    clicks submit button
        submit button calls ajax function
        ajax function sends username & password to check_user.py
        check_user.py checks if username & password are correct
            if not correct, return JSON formatted error message
            else, correct
                create session ID (SID)
                place SID in cookie
                return cookie to ajax function
                redirect user to welcome.html

welcome.html
    on page load, ajax function requests user's cookie
        if no cookie, redirect to login.html
        else, ajax function sends cookie to check_user.py
            check_user.py opens cookie & verifies the SID
                if not correct, redirect user to login.html
                else, correct
                    redirect user to welcome.html

I think I am misunderstanding how ajax is supposed to handle the returned cookie information. It is also possible that I am misunderstanding other parts, too. Please clarify :-)

I think I will follow this document when writing my Python session code. Sound ok?
I am considering using jQuery for the AJAX stuff and other Javascript coding.

Thank you!

  • 写回答

1条回答 默认 最新

  • weixin_33691700 2011-10-08 16:17
    关注

    Remember that the AJAX request is the same as any other HTTP request to the server. The session is maintained on the server side, but as far as the server can tell, a request from the browser is a request from the browser. An AJAX request can get and set the cookie just like any other request can. The method you've outlined above should work fine. Alternately, you could check for the existence of the session on your front page, and write the cookie then.

    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记