我得到無效標籤錯誤,現在我升級了我的jQuery。我讀到,新的1.5版本現在對JSON編碼更加嚴格。雖然我使用Zend_Json :: encode(array(「content」=> $ result));對HTML進行編碼,所以會這樣認爲。jQuery 1.5 - JSON錯誤無效標籤
我在手冊中看到它已被更新,儘管無法在沒有解析錯誤的情況下使用它。 jQuery JSON Manual
Error: invalid label
Source File: http://domain.local/register/#
Line: 0, Column: 1
Source Code:
{"content":"<div id=\"captcha_captcha.json\">\r\n<div class=\"input\">\r\n\t<div class=\"text-small\"><input type=\"text\" size=\"12\" name=\"captcha.json[input]\" id=\"captcha_id\" \/><\/div> \r\n\t<img src=\"http:\/\/domain.local\/tmp\/captchas
這是我的JavaScript代碼。
function regenerateCaptcha<?php echo $params["name"]?>(){
captcha_id = "captcha_<?php echo $params["name"]?>";
$.getJSON("/default/captcha/regeneratecaptcha/name/<?php echo $params["name"]?>.json", function(data){
success : pasteCaptcha<?php echo $params["name"]?>
});
}
function pasteCaptcha<?php echo $params["name"]?>(json){
$("#captcha_<?php echo $params["name"]?> .input").replaceWith(json.content);
}
當我查看該網址的來源:
{"content":"<div id=\"captcha_captcha\">\r\n<div class=\"input\">\r\n\t<div class=\"text-small\"><input type=\"text\" size=\"12\" name=\"captcha[input]\" id=\"captcha_id\" \/><\/div> \r\n\t<img src=\"http:\/\/fantasyolympics.local\/tmp\/captchas\/6498c75d8e3f9b8ed2cbb7a066f962a6.png\" class=\"captcha\" width=\"185\" height=\"36\" \/>\r\n <input type=\"hidden\" name=\"captcha[id]\" value=\"6498c75d8e3f9b8ed2cbb7a066f962a6\" \/> \r\n <a href=\"#\" onclick=\"regenerateCaptchacaptcha();return false;\" class=\"captcha_refresh\" title=\"Try a new code\"><\/a>\t\r\n<\/div>\r\n<\/div>\r\n<script type=\"text\/javascript\">\r\n\tfunction regenerateCaptchacaptcha(){\r\n\t\tcaptcha_id = \"captcha_captcha\";\r\n\r\n\t\t$.getJSON(\"\/default\/captcha\/regeneratecaptcha\/name\/captcha.json\", function(data){\r\n success : pasteCaptchacaptcha\t });\r\n\t} \r\n\t\r\n\tfunction pasteCaptchacaptcha(json){\r\n\t\t$(\"#captcha_captcha .input\").replaceWith(json.content);\r\n\t}\r\n<\/script>\r\n"}
我不完全確定是什麼問題,但http://stackoverflow.com/questions/1230897/using-jquery-to-get-json-data-returns-invalid-label-error是一個相關的問題。此外,由於您正在傳輸JSON編碼的HTML而不是數據,因此您也可以切換爲純HTML,而不會遇到編碼問題。 – Daff 2011-03-22 17:22:20