simplejson 双重转义数据导致无效的 JSON 字符串

发布于 2024-12-17 02:19:11 字数 2527 浏览 0 评论 0 原文

我有一个简单的表格来管理我店里的制造商。发布表单后,ajax 调用将包含更新数据的 json 返回到表单。问题是,返回的字符串无效。看来是双逃了。奇怪的是,整个商店都采用类似的方法,没有任何问题。我还使用 jquery 1.6 作为 javascript 框架。

模型包含 3 个字段:名称字符、描述文本和制造商徽标图像字段。

函数:

def update_data(request, manufacturer_id):
    """Updates data of manufacturer with given manufacturer id.
    """
    manufacturer = Manufacturer.objects.get(pk=manufacturer_id)
    form = ManufacturerDataForm(request.FILES, request.POST, instance=manufacturer)

    if form.is_valid():
        form.save()

    msg = _(u"Manufacturer data has been saved.")

    html = [
        ["#data", manufacturer_data_inline(request, manufacturer_id, form)],
        ["#selectable-factories-inline", selectable_manufacturers_inline(request, manufacturer_id)],
    ]

    result = simplejson.dumps({
        "html": html
    }, cls=LazyEncoder)
    return HttpResponse(result)

控制台中的错误:无效 JSON 的错误:

未捕获的异常:无效 JSON: {"html": [["#data", "\n

Dane

\n
\n \n
\n
\n \n
\n
\n <标签for="\"id_name\"">Nazwa:\n
\n \n \n
\n <输入 id="\"id_name\"" name="\"名称\"" maxlength="\"50\"类型=\"文本\"">\n
  • 极威马根
\n
\n \n
\n\n
\n
\n :\n
\n \n \n
\n <输入名称 =“\"图像\"” id=“\"id_image\"”类型=“\"文件\"”>\n < ;/div>\n \n
\n\n
\n
\n :\n \n \n \n
\n

有什么想法吗?

I have a simple form for managing manufacturers in my shop. After posting form, ajax call returns json with updated data to the form. Problem is, that the returned string is invalid. It looks like it was double-escaped. Strangely similar approach across the whole shop works without any problems. I'm also using jquery 1.6 as javascript framework.

Model contains of 3 fields : char for name, text for description and image field for manufacturer logo.

The function :

def update_data(request, manufacturer_id):
    """Updates data of manufacturer with given manufacturer id.
    """
    manufacturer = Manufacturer.objects.get(pk=manufacturer_id)
    form = ManufacturerDataForm(request.FILES, request.POST, instance=manufacturer)

    if form.is_valid():
        form.save()

    msg = _(u"Manufacturer data has been saved.")

    html = [
        ["#data", manufacturer_data_inline(request, manufacturer_id, form)],
        ["#selectable-factories-inline", selectable_manufacturers_inline(request, manufacturer_id)],
    ]

    result = simplejson.dumps({
        "html": html
    }, cls=LazyEncoder)
    return HttpResponse(result)

The error in console : error with invalid JSON :

uncaught exception: Invalid JSON: {"html": [["#data", "\n<h2>Dane</h2>\n<div class="\"manufacturer-image\"">\n \n</div>\n<form action="\"/manage/update-manufacturer-data/1\"" method="\"post\"">\n \n <div class="\"field\"">\n <div class="\"label\"">\n <label for="\"id_name\"">Nazwa</label>:\n </div>\n \n \n <div class="\"error\"">\n <input id="\"id_name\"" name="\"name\"" maxlength="\"50\"" type="\"text\"">\n <ul class="\"errorlist\""><li>Pole wymagane</li></ul>\n </div>\n \n </div>\n\n <div class="\"field\"">\n <div class="\"label\"">\n <label for="\"id_image\"">Zdjecie</label>:\n </div>\n \n \n <div>\n <input name="\"image\"" id="\"id_image\"" type="\"file\"">\n </div>\n \n </div>\n\n <div class="\"field\"">\n <div class="\"label\"">\n <label for="\"id_description\"">Opis</label>:\n </div>\n \n \n <div>\n <textarea id="\"id_description\"" rows="\"10\"" cols="\"40\"" name="\"description\""></textarea>\n </div>\n \n </div>\n \n <div class="\"buttons\"">\n <input class="\"ajax-save-button" button\"="" type="\"submit\"">\n </div>\n</form>"], ["#selectable-factories-inline", "\n <div>\n <a class="\"selectable" selected\"\n="" href="%5C%22/manage/manufacturer/1%5C%22">\n L1\n </a>\n </div>\n\n <div>\n <a class="\"selectable" \"\n="" href="%5C%22/manage/manufacturer/4%5C%22">\n KR3W\n </a>\n </div>\n\n <div>\n <a class="\"selectable" \"\n="" href="%5C%22/manage/manufacturer/3%5C%22">\n L1TA\n </a>\n </div>\n\n"]]}

Any ideas ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诠释孤独 2024-12-24 02:19:11

json 文本区域内有双引号及其 html 编码。例如,您的所有类属性在错误输出中都是这样的:

class="\"e;classname\"e;"

上面的内容应为:

class=\"classname\"

原始 json.dumps 将输出以下内容:

>>> json.dumps(["#data", '<div class="classname"></div>'])
'["#data", "<div class=\\"classname\\"></div>"]'

我怀疑您的 manufacturer_data_inline 或 < code>selectable_manufacturers_inline 调用会使您的引号加倍 "\"e; 或者 LazyEncoder 类做错了什么。

There are both double quote and its html encoding inside your json text regions. For example all your class attributes are like this in the error output:

class="\"e;classname\"e;"

The above should read:

class=\"classname\"

A raw json.dumps will output this:

>>> json.dumps(["#data", '<div class="classname"></div>'])
'["#data", "<div class=\\"classname\\"></div>"]'

I suspect that either your manufacturer_data_inline or selectable_manufacturers_inline calls are doubling up your quotes "\"e; or the LazyEncoder class is doing something wrong.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文