I got a JSP file where I define JSTL variable like this (form value) :
<c:set var="idEtape" value="${etapeForm.etape.idEtape}" scope="page"/>
<c:set var="idContact" value="${etapeForm.etape.idContact}" scope="page"/>
<c:set var="numeroDossierFoa" value="${etapeForm.dossier.numeroDossier}" scope="page"/>
<script type="text/javascript" src=myfile.js"/>"></script>
In my myfile.js file, I need to use these variable but I got an error who tell me that they are undefined.
I use these variables in an ajax call like this :
var request = $.ajax({
type: "POST",
url: "/myUrl/" + numeroDossier + "/" + idContact + "/" + idEtape,
cache: false
});
Where am I wrong ?
I got a JSP file where I define JSTL variable like this (form value) :
<c:set var="idEtape" value="${etapeForm.etape.idEtape}" scope="page"/>
<c:set var="idContact" value="${etapeForm.etape.idContact}" scope="page"/>
<c:set var="numeroDossierFoa" value="${etapeForm.dossier.numeroDossier}" scope="page"/>
<script type="text/javascript" src=myfile.js"/>"></script>
In my myfile.js file, I need to use these variable but I got an error who tell me that they are undefined.
I use these variables in an ajax call like this :
var request = $.ajax({
type: "POST",
url: "/myUrl/" + numeroDossier + "/" + idContact + "/" + idEtape,
cache: false
});
Where am I wrong ?
Share Improve this question edited Jun 18, 2014 at 12:44 Shaunak D 20.6k10 gold badges47 silver badges79 bronze badges asked Jun 18, 2014 at 12:38 user3469203user3469203 5472 gold badges9 silver badges22 bronze badges 03 Answers
Reset to default 5JSP variables cannot be used inside javascript file since JSP is server side and JS is client side
Try this:
<input id="idEtape" value="${etapeForm.etape.idEtape}" type="hidden"/>
In JS:
$("#idEtape").val();
You may try using ${}
to access jstl variables. In your jsp create js variables before the file import:
jsp
<script type="text/javascript">
var numeroDossier = '${etapeForm.dossier.numeroDossier}';
var idEtape = '${etapeForm.etape.idEtape}';
var idContact = '${etapeForm.etape.idContact}';
</script>
<script type="text/javascript" src=myfile.js"/>"></script>
js
var request = $.ajax(
{
type: "POST",
url: "/myUrl/" + numeroDossier + "/" + idContact + "/" +idEtape,
cache: false
});
You have to use EL expression to access this variable like below, provided that variable scope should be session :
<c:set var="idEtape" value="${etapeForm.etape.idEtape}" scope="session"/>
<c:set var="idContact" value="${etapeForm.etape.idContact}" scope="session"/>
<c:set var="numeroDossierFoa" value="${etapeForm.dossier.numeroDossier}" scope="session"/>
var request = $.ajax({type: "POST",
url: "/myUrl/" + '${numeroDossier}' + "/" + '${idContact}' + "/" + '${idEtape}',
cache: false
});