Because parse takes a string.This line

var ctg = JSON.parse({{=XML(ctg)}});

should be

var ctg = JSON.parse('{{=XML(ctg)}}');

or

var ctg = JSON.parse({{=repr(XML(ctg))}});

On Sep 3, 1:46 am, Vineet <vineet.deod...@gmail.com> wrote:
> I want to convert a list into javascript array.
> Referred to a related thread before posting.
>
> http://groups.google.com/group/web2py/browse_thread/thread/8a0048b368...
>
> Accordingly, my code is like this:--
>
> -----/controllers/test.py--------
> def cat():
>     ctg = ['A', 'B', 'C']
>     x = response.json(ctg)
>     return dict(ctg=x)
>
> Note:-    I did not use  "return response.json(dict(ctg=ctg))"
> because it directly returns the JSON object in browser view.
>
> -----/view/test/cat.html--------
> {{extend 'layout.html'}}
> <h1>ABC</h1>
>
> <script>
> function addOption(selectbox,text,value)
> {
> var optn = document.createElement("OPTION");
> optn.text = text;
> optn.value = value;
> selectbox.options.add(optn);}
>
> </script>
>
> <select name="ABC" id="ABC" size="1"></select>
>
> <script>
> var ctg = JSON.parse({{=XML(ctg)}});
> for (var i=0; i < ctg.length; ++i){
> addOption(document.ABC, ctg[i], ctg[i]);}
>
> </script>
> -----------------------------------
>
> In Opera Dragonfly, the following Error is reported.
>
> Uncaught exception: SyntaxError: JSON.parse: Unable to parse value:
> A,B,C
> Error thrown at line 2, column 0 inhttp://127.0.0.1:8000/mywheels/test/cat:
>     var ctg = JSON.parse(["A", "B", "C"]);
> -------------------------------
> I can't understand why it can't be parsed.
> Any idea what might be wrong in this?
>
> Thanks,
> Vineet

Reply via email to