console.log is not working

Asked

Viewed 1,536 times

0

What am I doing wrong? Why is the.log console 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

Follow 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>

1 answer

3


Probably <%= sListaCargo %> is returning a string, it would only take effect the way it did if it went to Javascript a format that was interpreted as Number, Object, Array, Boolean, in case it is a string it will be rendered 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 %>");

That’s because JSP runs on the back end and does not communicate directly with front-end, JSP is downloaded to the browser as if it were a real HTML page.

A detail regarding the console.log, you can switch to it:

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

For the console.log accepts multiple parameters, but is only suggestion, does not affect the main functionality.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.