最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Access JSTL variable define in a jsp inside AJAX call - Stack Overflow

programmeradmin0浏览0评论

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 0
Add a ment  | 

3 Answers 3

Reset to default 5

JSP 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
    });
发布评论

评论列表(0)

  1. 暂无评论