$( document ).ready(function() {
    console.log( "ready!" );
    var ilceler = "${ilceler}";
});


它不会显示“ Ready!”。进行控制台。当我用chrome调试时,我看到了

$( document ).ready(function() {
    console.log( "ready!" );
    var subChildList = "";
    ilceler = JSON.parse(" [
{"id":"xx","tag":"xx","name":"xxx","fatherid":"66","fathertag":"il"},
{"id":"xx","tag":"xx","name":"xxx","fatherid":"66","fathertag":"il"},
{"id":"xx","tag":"xx","name":"xxxxx","fatherid":"66","fathertag":"il"},
{"id":"xx","tag":"xx","name":"xxxx","fatherid":"66","fathertag":"il"},
{"id":"x","tag":"xx","name":"xxx xxx","fatherid":"66","fathertag":"il"},
{"id":"xx","tag":"xx","name":"xxxx","fatherid":"66","fathertag":"il"},
{"id":"xx","tag":"xx","name":"xxx","fatherid":"66","fathertag":"il"},
{"id":"xx","tag":"xx","name":"xxxx","fatherid":"66","fathertag":"il"},
{"id":"xx","tag":"xx","name":"xxx","fatherid":"66","fathertag":"il"},
{"id":"xx","tag":"xx","name":"xx\/xxx","fatherid":"66","fathertag":"il"},
{"id":"xx","tag":"xx","name":"xxx","fatherid":"66","fathertag":"il"},
{"id":"xx","tag":"xx","name":"xxx","fatherid":"66","fathertag":"il"},
{"id":"xx","tag":"xx","name":"xxx","fatherid":"66","fathertag":"il"},
{"id":"xx","tag":"xx","name":"xxxx","fatherid":"66","fathertag":"il"}]");
});


我的数据是正确的,但是为什么此功能不起作用。
注意:我的数据包含UTF-8字符。
注意2:我可以使用字符串代替json。

var a = ${tag}
console.log(a) >> "It's my tag"

最佳答案

在我看来,您需要删除变量周围的引号。

所以应该

var ilceler = ${ilceler};


JSP / JSTL解释器将保留这些引号,因此您的对象将被视为String值而不是JSON对象。另外,如果第二个输出是浏览器中的输出,则可以看到它不是有效的JSON。

07-28 03:07
查看更多