jQuery 1.5-JSON错误无效标签 [英] jQuery 1.5 - JSON error invalid label

查看:113
本文介绍了jQuery 1.5-JSON错误无效标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经升级了jQuery,但收到了无效标签错误.我读到新的1.5版本现在对JSON编码更加严格.虽然我使用Zend_Json :: encode(array("content" => $ result));对HTML进行编码,以至于这样做.

I am getting the invalid label error now I have upgraded my jQuery. I read that the new 1.5 version is now more strict on the JSON encoding. Although I use Zend_Json::encode(array("content"=>$result)); to encode the HTML so would of thought this would .

我在手册中读到它已被更新,尽管无法在没有解析错误的情况下使它工作. jQuery JSON手册

I read in the manual that it has been updated although cant get this to work without parse errors. 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代码.

This is my JavaScript code.

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);
    }

当我查看URL的来源时:

And when I view the source of the URL:

{"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"}

推荐答案

我刚刚解决了一个类似的问题.

I solved a similar problem just now.

jQuery引发无效标签错误的原因是它希望服务以JSONP格式而不是JSON格式进行响应.如果您的服务以JSON格式响应,您将收到此错误消息.

The reason jQuery throws an invalid label error is that it expects services to respond in JSONP format not JSON format. If your service responds in JSON format you will get this error message.

此问题的解决方法是对服务进行异步调用,然后使用jQuery.parseJSON解析结果.要调用以JSON格式响应的服务,您的代码应如下所示:

The workaround for this is to make an asynchronous call to your service and then parse your result with jQuery.parseJSON. To call a service that responds in JSON format your code should look something like this:

    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

这篇关于jQuery 1.5-JSON错误无效标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆