0

The below code is intended to return json object from arraylist but it is not showing alert. there is no js error...

<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript" src="json.js" />
<script type="text/javascript" src="json2.js" />
<script type="text/javascript">
var my_info= {};
my_info["john"]="1a";
my_info["joseph"]="2b";
my_info["helen"]="3c";

var val = JSON.stringify(my_info);

alert(val);
</script>
</head>
<body>

</body>
</html>
Aquarius24
  • 1,806
  • 6
  • 33
  • 61
  • remove the links having json.js and json2.js. The alert doesn't show if they are not available. – zs2020 Aug 22 '13 at 04:01
  • @sza That's false. A 404 error will not block the other js code running. The answer is that [the script tags do NOT allow self closing](http://stackoverflow.com/a/18371453/1420197). – Ionică Bizău Aug 22 '13 at 04:11

1 Answers1

1

Script tags don't allow self-closing (<script ... />). You have to use <script src="..."></script> instead.

See for more information this question.

Your code becomes:

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
        <script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
        <script type="text/javascript" src="json.js"></script> <!-- Here -->
        <script type="text/javascript" src="json2.js"></script> <!-- And here -->
        <script type="text/javascript">
            var my_info= {};
            my_info["john"]="1a";
            my_info["joseph"]="2b";
            my_info["helen"]="3c";

            var val = JSON.stringify(my_info);

            alert(val);
        </script>
    </head>
    <body>
    </body>
</html>

JSFIDDLE (ignore the warnings)

Community
  • 1
  • 1
Ionică Bizău
  • 109,027
  • 88
  • 289
  • 474