2011-11-19 49 views
0

我有一個簡單的表單來管理我店裏的製造商。在發佈表單之後,ajax調用將帶有更新數據的json返回給表單。問題是,返回的字符串是無效的。看起來它是雙重逃脫的。整個車間奇怪的類似方法沒有任何問題。我也使用jQuery 1.6作爲JavaScript框架。simplejson double escapes data caused invalid JSON string

模型包含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"]]}

任何想法?

+0

我記錄了結果變量,所以基本上只是返回到模板的東西,它正確地轉義。所以看起來響應在被jquery的parseJSON函數處理之前在某處被轉義。現在我真的很困惑。 –

+0

錯誤在哪裏被捕獲?它在服務器端還是在瀏覽器中? –

回答

0

在你的json文本區域內有雙引號和它的html編碼。例如,所有的類屬性都是這樣的錯誤輸出:

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

以上應爲:

class=\"classname\" 

原始json.dumps將輸出這樣的:

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

我懷疑或者您的manufacturer_data_inlineselectable_manufacturers_inline調用將您的報價加倍"\&quote;或LazyEncoder類執行錯誤。

相關問題