jquery - interaction Django and Ajax -
i'm trying send data server , use ajax
function stats(e){ jquery.ajax({ type: "post", url:"stats", data:{'csrfmiddlewaretoken': document.getelementsbyname('csrfmiddlewaretoken')[0].value, 'test':{}}, success: function(data) {alert("congratulations!"+data);}, error: function(data) { alert("please report error: "+data.responsetext);} }); }
function in views.py:
def stats(request): if request.is_ajax(): if request.user.is_authenticated(): if request.post: return httpresponse(request.post['test']) else: return httpresponse("post_no_exists") else: return httpresponse("no authenticate") else: raise http404
django raise multivaluedictkeyerror 'key "test" not found in querydict'. when change "test":{} -> "test":1 succeds. whats error?
here:
... data: { 'csrfmiddlewaretoken': ..., 'test': {} } ...
you're trying send empty object literal part of post request. jquery can't figure out value send (empty object? mean?), doesn't send test
parameter @ all. thus, on server side, trying access request.post['test']
throws keyerror.
when change test different literal (in case number), jquery can encode , send value.
Comments
Post a Comment