我有一个简单的表格来管理我店里的制造商。发布表单后,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", "\nDane
\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 ?
发布评论
评论(1)
json 文本区域内有双引号及其 html 编码。例如,您的所有类属性在错误输出中都是这样的:
上面的内容应为:
原始
json.dumps
将输出以下内容:我怀疑您的
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:
The above should read:
A raw
json.dumps
will output this:I suspect that either your
manufacturer_data_inline
orselectable_manufacturers_inline
calls are doubling up your quotes"\"e;
or the LazyEncoder class is doing something wrong.