0

ショップでメーカーを管理するための簡単なフォームがあります。フォームを投稿した後、ajax 呼び出しは、更新されたデータを含む json をフォームに返します。問題は、返された文字列が無効であることです。どうやらダブルエスケープされたようです。不思議なことに、ショップ全体で同様のアプローチが問題なく機能します。また、javascript フレームワークとして jquery 1.6 を使用しています。

モデルには、名前の文字、説明のテキスト、製造元のロゴの画像フィールドの 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 によるエラー:

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

何か案は ?

4

1 に答える 1

0

jsonテキスト領域内には二重引用符とそのhtmlエンコーディングの両方があります。たとえば、エラー出力では、すべてのクラス属性は次のようになります。

class="\&quote;classname\&quote;"

上記は読む必要があります:

class=\"classname\"

rawjson.dumpsはこれを出力します:

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

あなたのmanufacturer_data_inlineまたはselectable_manufacturers_inline呼び出しがあなたの引用符を倍増させている"\&quote;か、LazyEncoderクラスが何か間違ったことをしているのではないかと思います。

于 2011-11-19T01:44:14.693 に答える