我有一个简单的表格来管理我店里的制造商。发布表单后,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错误:
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"]]}
有什么想法吗?
答案 0 :(得分:0)
在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
或selectable_manufacturers_inline
来电正在加倍引用"\"e;
或者LazyEncoder类做错了。