console.log is not working

0

What am I doing wrong? Why is console.log not working when I enter the variable via JSP? The following error appears:

  

index.jsp: 1 Uncaught SyntaxError: missing) after argument list

at eval (<anonymous>)

at jquery-1.7.2.min.js:2

The following is the code below:

<select id="cargo" name="cargo">
    <option value=""></option>                                                          
    <% 
       List<String> cargos = FuncApp.obterCargos();
       String sListaCargo;
       for(int i = 0; i < cargos.size(); i++) {
           sListaCargo = cargos.get(i);
    %>
    <script>
        console.log("sListaCargo:" + <%= sListaCargo %> );
    </script>
    <option value='<%= sListaCargo %>' <%= (sCargo.equals(sListaCargo)) ? "selected='selected'" : "" %>> <%= sListaCargo %> </option>                                                               
    <% 
       } //fecha for
    %>      
</select>
    
asked by anonymous 13.12.2017 / 15:25

1 answer

2

Probably <%= sListaCargo %> is returning a string, it would only have effect the way it did if it were to JavaScript a format that was interpreted as Number , Object , Array , Boolean , in case is a string it will render as:

 console.log("sListaCargo:" + foo bar );

The foo bar would be an example of text, so what you have to do is:

 console.log("sListaCargo: <%= sListaCargo %>");

This is because JSP runs in the backend and does not communicate directly with the front end, the JSP is downloaded to the browser as if it were an actual HTML page.

A detail, if it's about console.log , you can change to this:

 console.log("sListaCargo", "<%= sListaCargo %>");

Because console.log accepts multiple parameters, but it's just a suggestion, it does not affect the main functionality.

    
13.12.2017 / 16:18