2017-07-08 24 views
1

加载JSON数据我一直在关注烬快速入门指南创建显示一些数据的应用程序(https://guides.emberjs.com/v2.13.0/getting-started/quick-start/),而是显示只是科学家的名字JavaScript数组的,我想展示的产品来自以下json。灰烬应用程序无法从本地文件

我已经把JSON文件中的公用文件夹。

它看起来像:

{ 
    "products": [ 
    { 
     "_id": "58ff60ffcd082f040072305a", 
     "slug": "apple-tree-printed-linen-butterfly-bow-tie", 
     "name": "Apple Tree Printed Linen Butterfly Bow Tie ", 
     "description": "This fun 40 Colori Apple Tree Printed Linen Butterfly Bow Tie features a design of various apple trees built from tiny polka dots. The back of this bow tie features a polka dot print in complementing colours which when the bow tie is tied will pop out from behind making for a subtle yet unique detail. The playful design, stand out natural linen texture, and colourful combinations make this bow tie a perfect accessory for any outfit!\n", 
     "standard_manufacturer": "58cbafc55491430300c422ff", 
     "details": "Size: Untied (self-tie) bow tie with an easily adjustable neck strap from 13-3/4'' to 19'' (35 to 48 cm)\nHeight: 6 cm\nMaterial: Printed 100% linen\nCare: Dry clean\nMade in Italy", 
     "sizes": [ 
     { 
      "value": "Violet", 
      "size": "57722c80c8595b0300a11e61", 
      "_id": "58ff60ffcd082f0400723070", 
      "marked_as_oos_at": null, 
      "quantity": -1, 
      "stock": true, 
      "id": "58ff60ffcd082f0400723070" 
     }, 

等。

我对路线的模型显示列表代码如下

import Ember from 'ember'; 

export default Ember.Route.extend({ 
    model() { 
     //return ['Marie Curie', 'Mae Jemison', 'Albert Hofmann']; 
     return Ember.$.getJSON("/products.json"); 
    } 
}); 

我已经完全按照教程除了在scientists.js的

return Ember.$.getJSON("/products.json"); 

线。我的数据没有被显示,并且我在余烬检查员中得到的错误是

compiled.js:2 Uncaught TypeError: Failed to execute 'getComputedStyle' on 'Window': parameter 1 is not of type 'Element'. 
    at E (compiled.js:2) 
    at Object.u (compiled.js:25) 
    at compiled.js:25 
E @ compiled.js:2 
u @ compiled.js:25 
(anonymous) @ compiled.js:25 

我是非常新的烬并且与js相当新。任何帮助感谢!谢谢!

回答

1

灰烬附带开发服务器(本地主机:4200),并且它不能像一个网络服务器中使用,它不能被用来响应Ajax请求。你不能使用localhost:4200端点工作Ember.$.getJSON("/products.json")

你需要后端网络服务器的任何返回响应。如果您目前还没有这个功能,那么为了开发目的,您需要使用动态渲染数据,然后我更愿意使用ember-cli-mirage插件。

http://www.ember-cli-mirage.com/docs/v0.3.x/

0

有关放置JSON在应用程序文件夹和进口什么呢?

import Ember from 'ember'; 
import yourData from 'your-app/json-file-name.js'; 

export default Ember.Route.extend({ 
    model() { 
    return yourData; 
    } 
}); 

https://ember-twiddle.com/afb9d71933322860938b3936d617a776 - 你可以用它来尝试,并得到一个以.json工作...

有一个线程在这里太: https://discuss.emberjs.com/t/how-to-import-data-from-json-file-to-ember-app

但是...不要指望它正确地加入到ember-data存储中 - 并且在许多情况下可以像你期望的那样工作。

+0

所有的“故事”的数据为这个应用程序我建立了从应用程序目录中的静态文件加载/所以我知道这是可能的是这样的工作。 http://judge.justicefor.us/ – sheriffderek

+0

你可能必须重新格式化JSON是灰烬友好 – sheriffderek

+0

您的意思是线路2:从“你的应用程序内/ JSON-文件name.json” 进口yourData – Yiannis