开发者

Assigning external JSON to a var

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.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.


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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜