开发者

Django forms + errors in JSON + jquery syntax error

I'm using a simple Django form with JSON validation. Following Jacobian method on how to expose form errors into a JSON object, I used this function:

def errors_to_json(errors):
# Force error strings to be un-lazied.
return simplejson.dumps(dict((k, map(unicode, v)) 开发者_运维知识库for (k,v) in errors.iteritems()))

However, when a form fails to validate, I encounter this jQuery syntax error:

Uncaught SyntaxError: Unexpected token :
c.extend.globalEvaljquery.min.js:29
c.extend.httpDatajquery.min.js:132
c.extend.ajax.g.x.onreadystatechange

I think this SyntaxError is due to a bad JSON response, but I cannot find errors in this JSON:

{"title": ["This field is required."]}

I've tried with jQuery 1.5.1, 1.4.4, 1.4.2, using the

$.post()

method.


Here I post the solution:

switched from

$.post()

to

$.ajax(url, function() {}, dataType:'JSON');

It worked.


$.post("test.php", { "func": "getNameAndTime" },
 function(data){
   console.log(data.name); // John
   console.log(data.time); //  2pm
 }, "json");

http://api.jquery.com/jQuery.post/

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜