将外部 JSON 分配给 var
我正在使用 JSON 中的值填充 Dojo 组合框下拉列表。 下面的代码工作得很好(内联 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>
但是,当我为 JSON 指定外部文件时,没有执行,也就是说填充了下拉列表。 外部文件是standard.txt,看起来像这样...
{
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"}
]};
我对dojo的HTML调用看起来像这样..
<div dojoType="dojo.data.ItemFileReadStore" jsId="xvarStore2" url="http://localhost:3000/static/standard.txt">
</div>
内联工作正常,但外部调用不行。抱歉,如果这是一个补救问题,但我如何读取外部文件并将其分配给“magicvars”。我只是不想用一堆内联 JSON 搞乱 HTML。
任何建议表示赞赏。 珍妮
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它不是有效的 JSON,因此不会用大多数
JSON.parse
实现进行解析。尝试引用键名称并去掉结尾的分号。在 Chrome 上,
生成方式
与此相同
,但使用有效的 JSON(请注意
"a"
周围的引号)返回预期结果。
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,
produces
as does
but with valid JSON (note the quotes around
"a"
)returns the expected result.
尝试将文件重命名为
standard.json
。我的猜测是,dojo 正在将您的文件作为纯文本字符串读取,因此不会解析 JSON。 (正如其他答案中指出的那样,这是无效的)
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)