What's the best way to handle session timeouts in ajax requests?

后端 未结 2 1205
忘了有多久
忘了有多久 2021-02-13 12:50

Consider this Django view which will get a list of items associated to the current user:

@login_required
def list_items(request, page_number=0):
    items = Pagi         


        
2条回答
  •  深忆病人
    2021-02-13 13:03

    I would handle it by having your session timeout method check whether or not it is being requested with AJAX. If it is ajax, return a 401 not authorized(or 403 forbidden or whatever status makes sense) status code with an empty json string. Next, in your javascript, bind a global ajaxError handler that checks for that status code and handles it appropriately.

提交回复
热议问题