SCRIPT1007: expected error ']' for eval expression when converting a string to an array

I am doing ajax, I have the following problem and javascipt fails with an eval expression. I am using IE9 browser. It gives error SCRIPT1007: Expected ']' when expressing eval. The following is a javascript function, and you can also find the problem that I have indicated with arrows.

<script type="text/javascript">
    function callAjax(){
        var xhr = new XMLHttpRequest();
        xhr.open("GET","ajax");
        xhr.send(null);
        xhr.onreadystatechange = function(){
        if(xhr.readyState == 4 ){
            if(xhr.status==200)
                var arrStr = xhr.responseText;

                //arrStr holds ['raj,'jay'] 
                ***var arr = eval(arrStr);------------->problem area***  
                var list ="";
                for (var i=arr; i<arr.length; i++){
                    list+=arr[i]+"<br>";
                }

                document.getElementById("msg").innerHTML=list;
            } else if ( xhr.status==404) {
                document.getElementById("msg").innerHTML="<h6 style='color: red'>Invalid    Path</h6>"
            }
        }
    }
</script>
0
source share
2 answers
['raj,'jay'] 

You don't seem to have a closing quote after raj.

0
source

If this line Arrayis specified by the user or other untrustworthy source, you should avoid eval()under any circumstances! (see what happens)

, serialize unserialize.

0

Source: https://habr.com/ru/post/1609898/


All Articles