简体   繁体   中英

JavaScript which inside JSP doesn't work when I used java tag

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

It doesn't print "Ready!" to console. When I debug with chrome, I saw

$( 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"}]");
});

My data is true, but why this function doesn't work. Note: My data includes UTF-8 characters. Note2: I can work with string instead of json.

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

I seems to me that you need to remove the quotes surrounding your variable.

So it should be

var ilceler = ${ilceler};

The JSP/JSTL interpreter will leave these quotes and hence your object will be regarded as a String value instead of a JSON object. Also if your second output is the output in the browser you can see it's not valid JSON.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM