Ember应用程序无法从本地文件加载json数据 [英] Ember app cannot load json data from local file

查看:105
本文介绍了Ember应用程序无法从本地文件加载json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在追踪ember快速入门指南,创建一个显示某些数据的应用程序( https://guides.emberjs.com/v2.13.0/getting-started/quick-start/ ),而不是只显示一个具有科学家名称的JavaScript数组,我想显示

I have been following the ember quick start guide to create an app that displays some data (https://guides.emberjs.com/v2.13.0/getting-started/quick-start/) but instead of displaying just a javascript array with scientists names, I want to display the products from the following json.

我将json文件放在公用文件夹中。

I have placed the json file in the public folder.

看起来像:

{
  "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"
        },

等等。

我用于显示列表路由模型的代码如下:

My code for the model of the route for displaying the list is as follows

import Ember from 'ember';

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

我已经遵循了这个教程,除了

I have followed the tutorial exactly except for the

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

我的数据没有显示,我在ember检查器中的错误是

line in scientists.js. My data is not being displayed and the error i get in the ember inspector is

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

我是非常新的与ember和相当新的js。任何帮助赞赏!谢谢!

I am very new with ember and fairly new with js. Any help appreciated! Thanks!

推荐答案

Ember附带开发服务器(localhost:4200),它不能像web服务器一样使用,不能用于响应ajax请求。你不能使这个工作 Ember。$。getJSON(/ products.json) with localhost:4200 endpoint。

Ember comes with Development server(localhost:4200) and it can't be used like a webserver and it can't be used to response ajax request. You can't make this work Ember.$.getJSON("/products.json") with localhost:4200 endpoint.

您需要后端任何Web服务器才能返回响应。如果你目前没有这个目的,那么为了开发目的,你需要使用动态渲染数据,
然后我更喜欢使用 ember-cli-mirage addon。

You need backend any webserver to return response. If you don't have that at present then for development purpose, you need to work with dynamic render data, then I prefer to use ember-cli-mirage addon.

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

这篇关于Ember应用程序无法从本地文件加载json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆