1

I am not sure it's about django, rather javascript and browser.

my view:

def set_lang(request, lang):
  request.session['lang'] = lang   

  # request.session.modified = True  # optional, to be pretty sure
  # request.session.save()  # the same

  return HttpResponse('ok')

in html, in js:

$.post('/set_lang', {'lang', 'EN'});   # it change session and works
location.reload()

somewhere while location.reload() the django session is "rollbacked" to previous state.

on chrome when I add own sleep method in js (1sec) before location.reload it works on firefox problem exists regardless.

I do not use cookies except session one. I tried cleanup browser cache, session, but without success. I can repeat this on different browser versions.

I have workaround but I am curious what happened.

when I remove from js location.reload(), and then do POST/GET request will be handled ok with correct session.

F5 refresh after set_lang also works always (session is changed correctly).

I compare http headers between location.reload and F5 refresh and they are the same.

4

1 回答 1

0

看起来location.reload不允许$.post

$.post(url, data);
// sleep(2000) // own func with timeout works only in chrome for me
location.reload();

我问自己发帖是否有足够的时间发送并在jQuery.post 文档中找到解决方案

当我将代码更改为:

$.post(url, data).done(function(data{window.location.reload();})

它现在可以在每个浏览器中使用 :)

于 2013-07-15T08:41:17.783 回答