Sunday, April 10, 2011

processing POST data from jquery

I am trying to get a jquery form validator working. I am stuck now
trying to work with the POST data. When i use a simple view for
testing:

def Code(request):
if request.method == 'POST':
message = request.POST
data = dict(ok=True, msg=message)
from django.utils import simplejson
return
HttpResponse(simplejson.dumps(data),mimetype='application/json')

The returned json is:
{"msg": {"code": [""]}, "ok": true}

This should be:
{"msg": "code", "ok": true}

The javascripts sends a POST with a dictionary containing an empty
key. How should i handle the post data?

I am trying to work out an example from this page:
http://jqueryfordesigners.com/using-ajax-to-validate-forms/

Here is the javascript i use:

$(document).ready(function () {
var validateUsername = $('#validatecode');
$('#id_code').keyup(function () {
var t = this;
if (this.value != this.lastValue) {
if (this.timer) clearTimeout(this.timer);
validateUsername.removeClass('error').html('<img src="/media/
plaatjes/ajax-loader.gif" height="16" width="16" /> check$

this.timer = setTimeout(function () {
$.ajax({
url: '/code-validatie/',
data: t.value,
dataType: 'json',
type: 'post',
success: function (j) {
validateUsername.html(j.msg);
}
});
}, 200);

this.lastValue = this.value;
}
});
});

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home


Real Estate