Question

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

Was it helpful?

Solution

It's not valid JSON, so will not parse with most JSON.parse implementations. Try quoting the key names and getting rid of the trailing semicolon.

On Chrome,

JSON.parse('{ a: "b" }')

produces

SyntaxError: Unexpected token ILLEGAL

as does

JSON.parse('{ a: "b" };')

but with valid JSON (note the quotes around "a")

JSON.parse('{ "a": "b" }')

returns the expected result.

OTHER TIPS

Try renaming your file to standard.json.

My guess is that dojo is reading your file as a plain text string, and therefor not parsing the JSON. (Which, as pointed out in other answers, is not valid)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top