JavaScript 错误:标签无效?这是什么意思?

发布于 2024-10-15 08:33:10 字数 2969 浏览 5 评论 0原文

不用担心!它看起来比实际更多的文字;-)所以请继续阅读!

主要问题是:

  • 错误:无效标签是什么意思?
  • 我的脚本错误在哪里?

现在极客的东西开始了;-):

我有一个脚本,它从服务器加载语言数组的项目(通过 js 中的 lang_keys 定义的项目 - 见上文)(服务器提供该语言数组的 JSON 版本)数组),用于当前语言。

使用 JS translate(""); 函数,您可以获取当前语言所需的文本。 更新:jquery 1.5 未缩小(旧版:jquery.js(v1.4.1 - 缩小))已加载,lang.js 也已加载。

所以我收到一个错误,我只是找不到它,

我必须添加: translate() 函数可以正常工作,并且 JS 不会中断。所有功能均按预期工作。 我没有任何其他错误,并且所有 JS 都工作正常...但我不想在未来感到惊讶 - 所以我需要摆脱该错误。

控制台输出chrome(在ajax请求[/query/js_lang/json]之后列出):

Uncaught SyntaxError: Unexpected token :    jQuery.jQuery.extend.globalEvaljquery.js:602
jQuery.ajaxSetup.converters.text            scriptjquery.js:6992
ajaxConvert                                 jquery.js:6884
done                                        jquery.js:6454
jQuery.ajaxTransport.send.callback          jquery.js:7252
jQuery.ajaxTransport.send                   jquery.js:7261
jQuery.extend.ajax                          jquery.js:6646
(anonymous function)                        lang.js:116

firefox中的控制台输出(在ajax请求[/query/js_lang/json]之后列出):

invalid label
{"js_accept_terms":"Du musst unseren A...:"Kontaktname","js_agent_email":"Konta

谁能告诉我我到底错误是什么:无效标签意味着什么?

我的脚本(lang.js)看起来像这样:

var month_names = new Array();
var day_names = new Array();

var lang_keys = new Array(   
    "js_accept_terms",
    ...
    "nope"
);

var translations = new Array();


function translate(key, replace){
    var translated = translations[key];

    if(replace != undefined){
        for(var i=0; i<replace.length; i++){
            translated = translated.replace(/\%1/, replace); 
        }
    }

    return translated;
}

$.ajax({ //this is line 116
   url: "/query/js_lang/json",
   type: "post",
   data: {keys: JSON.stringify(lang_keys)},
   timeout: 7000,
   success: function(data){
       var trans = jQuery.parseJSON(data);
       for(var key in trans){
           translations[key.replace(/^js\_/, "")] = trans[key];
       }
       month_names = new Array(translate("jan"), translate("feb"), translate("mar"), translate("apr"), translate("may"), translate("jun"), translate("jul"), translate("aug"), translate("sep"), translate("oct"), translate("nov"), translate("dec"));
       day_names   = new Array(translate("sun"), translate("mon"), translate("tue"), translate("wed"), translate("thu"), translate("fri"), translate("sat"));
   },
   error: function(){
       out_message("Error. No Language loaded!", "Error");
   },
   async: false
});

out_message()显示一个css-样式化的div。我在网站的其他部分使用该功能,它运行没有任何问题。

我删除了 lang_keys 中的项目,其中返回值具有特殊字符,如“:”、元音变音、斜杠等...说:我仅使用字母数字值对其进行了测试,并得到了相同的错误.

抱歉我的英语不好;-)并感谢您的帮助

no worries! it looks like more text than it is ;-) so please go on reading!

The Main Questions are:

  • What does the error: invalid label mean?
  • Where is the error in my script?

now the geeky stuff begins ;-):

I have a script which loads items of an language-array (items defined via lang_keys in js - see above) from the server (server delivers a JSON version of that array), for the current language.

with JS translate("<synonym>"); function you get the required text in the current language. UPDATE: jquery 1.5 not minified (old: jquery.js (v1.4.1 - minified)) is loaded and lang.js too.

so I get an error, which I just can't find

I have to add: the translate() function works without errors and the JS is not interrupted. All functions work as desired.
i don't have any other errors and all the JS works fine ... but i don't want to be surprised in the futur - so i need to get rid of that error.

console output in chrome (which is listed after the ajax request [/query/js_lang/json]):

Uncaught SyntaxError: Unexpected token :    jQuery.jQuery.extend.globalEvaljquery.js:602
jQuery.ajaxSetup.converters.text            scriptjquery.js:6992
ajaxConvert                                 jquery.js:6884
done                                        jquery.js:6454
jQuery.ajaxTransport.send.callback          jquery.js:7252
jQuery.ajaxTransport.send                   jquery.js:7261
jQuery.extend.ajax                          jquery.js:6646
(anonymous function)                        lang.js:116

console output in firefox (which is listed after the ajax request [/query/js_lang/json]):

invalid label
{"js_accept_terms":"Du musst unseren A...:"Kontaktname","js_agent_email":"Konta

Can anyone tell me exactly what the error: invalid label means?

My script (lang.js) looks like that:

var month_names = new Array();
var day_names = new Array();

var lang_keys = new Array(   
    "js_accept_terms",
    ...
    "nope"
);

var translations = new Array();


function translate(key, replace){
    var translated = translations[key];

    if(replace != undefined){
        for(var i=0; i<replace.length; i++){
            translated = translated.replace(/\%1/, replace); 
        }
    }

    return translated;
}

$.ajax({ //this is line 116
   url: "/query/js_lang/json",
   type: "post",
   data: {keys: JSON.stringify(lang_keys)},
   timeout: 7000,
   success: function(data){
       var trans = jQuery.parseJSON(data);
       for(var key in trans){
           translations[key.replace(/^js\_/, "")] = trans[key];
       }
       month_names = new Array(translate("jan"), translate("feb"), translate("mar"), translate("apr"), translate("may"), translate("jun"), translate("jul"), translate("aug"), translate("sep"), translate("oct"), translate("nov"), translate("dec"));
       day_names   = new Array(translate("sun"), translate("mon"), translate("tue"), translate("wed"), translate("thu"), translate("fri"), translate("sat"));
   },
   error: function(){
       out_message("Error. No Language loaded!", "Error");
   },
   async: false
});

out_message() displays a css-styled div. I am using that function in other parts of the website, where it works without any problems.

I removed items in lang_keys where the return value has special characters like ':', umlauts, slashes and so on ... say: I tested it with only alphanumeric values and got the same error.

sorry for my English ;-) and thanks for your help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

护你周全 2024-10-22 08:33:10

我认为问题出在返回的 JSON:

{"js_accept_terms":"Du musst unseren A...:"Kontaktname","js_agent_email":"Konta

这应该看起来像这样:

{"js_accept_terms":"Du musst unseren A...:\"Kontaktname","js_agent_email":"Konta

服务器端可能有问题。您的 JSON 编码器不会转义双引号。

I think the problem is with the returned JSON:

{"js_accept_terms":"Du musst unseren A...:"Kontaktname","js_agent_email":"Konta

This should look something like this:

{"js_accept_terms":"Du musst unseren A...:\"Kontaktname","js_agent_email":"Konta

There may be a problem on the server side. Your JSON encoder does not escape double quotes.

盛夏尉蓝 2024-10-22 08:33:10

好吧,经过更多的研究和 StackOverflow 线程中的一些提示,我现在明白了:

问题是,由于某种原因,jQuery 将 ajax 接收到的数据解释为脚本(javascript) - 必须与 eval() 做一些事情 函数,它将第一个 json 对象解释为脚本,因为它看起来像 / 开头像一个对象实例。

仅当 ajax 调用在网站完全加载之前完成时,才会发生这种情况(就我而言)。好吧,通过 ajax 调用内部一些 $(function() { ... }); 代码,到目前为止没有问题。

技巧是在 php 脚本中的 json 字符串周围添加小括号

$return_value = "(" .json_encode($translation) . ")";

并在接收到 ajax-success 回调后将其

$.ajax({    
    ...,
    success: function(data){
           data = data.substring(1,data.length-1); // for label error reason. getting json in breaktes,, which must be removed again
           var trans = jQuery.parseJSON(data);
          ...

删除关于标签的信息:这里有一些信息

Well, after some more research and some hints in StackOverflow threads i figured it out now:

The Problem is, that jQuery interpretes the ajax received data as a script (javascript) for some reason - must have something todo with eval() function, which interpretes the first json object as script because it looks like / starts like an object instance.

This occures (in my case) only if the ajax call is done before the site is loaded completely. well, with ajax calls inner some $(function() { ... }); code there are no problems so far.

the trick was to add brakets around the json string in the php script

$return_value = "(" . json_encode($translation) . ")";

and remove them after receiving in the ajax-success callback

$.ajax({    
    ...,
    success: function(data){
           data = data.substring(1,data.length-1); // for label error reason. getting json in breaktes,, which must be removed again
           var trans = jQuery.parseJSON(data);
          ...

just fyi about labels: here some information about that

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文