【问题标题】:Django forms + errors in JSON + jquery syntax errorDjango 表单 + JSON 中的错误 + jquery 语法错误
【发布时间】:2011-08-06 08:02:00
【问题描述】:

我正在使用带有 JSON 验证的简单 Django 表单。按照关于如何将表单错误暴露到 JSON 对象中的 Jacobian 方法,我使用了这个函数:

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()))

但是,当表单验证失败时,我遇到了这个 jQuery 语法错误:

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

我认为这个 SyntaxError 是由于错误的 JSON 响应造成的,但我在这个 JSON 中找不到错误:

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

我尝试过使用 jQuery 1.5.1、1.4.4、1.4.2,使用

$.post()

方法。

【问题讨论】:

    标签: jquery django forms


    【解决方案1】:

    我在这里发布解决方案:

    转自

    $.post()
    

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

    成功了。

    【讨论】:

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

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

      【讨论】:

        猜你喜欢
        • 2012-08-28
        • 2013-11-28
        • 2013-06-02
        • 2023-04-06
        • 2011-07-30
        • 1970-01-01
        • 2021-09-07
        • 1970-01-01
        • 2013-05-19
        相关资源
        最近更新 更多