I am populating a Dojo Combobox dropdown with values from JSON.
The code below works just fine (inline JSON)…..
<script>
var magicvars = {
identifier: 'name',
label: 'name',
items: [
{name: "ZCCN_NO_1", label: "<img width='16px' height='16px' src='http://localhost:3000/static/images/eight_ball_16x16.png'/>ACCN_NO_1"},
{name: "CR_Local_ID", label:"<img width='16px' height='16px' src='http://localhost:3000/static/images/eight_ball_16x16.png'/>CR_Local_ID"}
]};
</script>
<div dojoType="dojo.data.ItemFileReadStore" data="magicvars" jsId="xvarStore2"></div>
However when I specify an external file for the JSON, no go, which is to say that the dropdown populates.
The external file is standard.txt and looks like this…
{
identifier: 'name',
label: 'name',
items: [
{name: "ZCCN_NO_1", label: "<img width='16px' height='16px' src='http://localhost:3000/static/images/eight_ball_16x16.png'/>ACCN_NO_1"},
{name: "CR_Local_ID", label:"<img width='16px' height='16px' src='http://localhost:3000/static/images/eight_ball_16x16.png'/>CR_Local_ID"}
]};
My HTML call to dojo the looks like this..
<div dojoType="dojo.data.ItemFileReadStore" jsId="xvarStore2" url="http://localhost:3000/static/standard.txt">
</div>
Inline works fine but the external call does not. Apologies if this is a remedial question but how can I read the external file and assign it to “magicvars”. I just don’t want to clutter up the HTML with a bunch of inline JSON.
Any advice is appreciated.
Janie
It’s not valid JSON, so will not parse with most
JSON.parseimplementations. Try quoting the key names and getting rid of the trailing semicolon.On Chrome,
produces
as does
but with valid JSON (note the quotes around
"a")returns the expected result.