2011-03-22 121 views
0

我得到無效標籤錯誤,現在我升級了我的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"} 
+1

我不完全確定是什麼問題,但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

回答

1

我剛纔解決了類似的問題。

jQuery拋出無效標籤錯誤的原因是它期望服務以JSONP格式而不是JSON格式進行響應。如果您的服務以JSON格式響應,您將收到此錯誤消息。

解決方法是對服務進行異步調用,然後使用jQuery.parseJSON解析結果。要調用JSON格式的響應服務代碼應該是這個樣子:

function createRequest() { 
     try { return new XMLHttpRequest(); } catch(e) {} 
     try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {} 
     alert("XMLHttpRequest not supported"); 
     return null; 
    } 

    var request = createRequest(); 
    request.open(「GET」, HTTP://URL.of.service.to.call, true); 
    request.onreadystatechange = callback; 
    request.send(null); 

    function callback() { 
     if(request.readyState != 4) { return } 
     ObjectYouWant = $.parseJSON(request.responseText); 
    } 

我通過適應於AjaxPatterns.org發現代碼得到這個解決方案在我的應用程序工作和jQuery.com

1

有這個問題時,我從幾個版本升級jquery回來。

我正在使用jquery表單插件,所以我沒有直接控制解碼json字符串。

我發現這個問題是相關的jquery 1.5,使用1.4.4(和形式插件v2.73),而不是它通過罰款。