【问题标题】:Not able to add data using ember-data Model无法使用 ember-data 模型添加数据
【发布时间】:2017-05-21 03:58:02
【问题描述】:

控制器/新客户

  actions: {
    addInfo: function() {
      var name = this.get('name');
      var e_mail = this.get('e-address');
      var emoney = this.get('e-money');
      var newCustomer = this.store.createRecord('customers', {
        name    : name,
        email   : e_mail,
        emoney  : emoney
      });
      newCustomer.save();
      alert("You\'re added");
    }

模型/customers.js

import DS from 'ember-data';

export default DS.Model.extend({
    name:DS.attr('string'),
    email:DS.attr('string'),
    emoney:DS.attr('number'),
    rev: attr('string')
});

模板/new-customer.hbs

<form class="form-horizontal">
    <div class="form-group control-group error">
        <label class="control-label" for="inputError">Name</label>
        {{input type="text" class="form-control" value=name placeholder = "Enter your name" size="15"}}
    </div>
    <div class="form-group">
        <label>Email Address</label>
        {{textarea type="text" class="form-control" value=e-address placeholder = "Enter your Email Address"}}
    </div>
    <div class="form-group">
        <label>E-Money</label>
        {{input type="number" class="form-control" value=e-money}}
    </div>
    <button {{action 'addInfo'}}type="button" class="btn btn-primary">Submit !!</button>
</form>

我错过了什么吗?单击提交按钮时我在控制台上出现的错误是:

加载资源失败:服务器响应状态为 404(未找到) vendor.js:27458 Ember 数据请求 POST /customers 返回 404 有效负载(文本/html;字符集=utf-8) 无法发布/客户

错误 在 AdapterError (http://localhost:4200/assets/vendor.js:83957:16) 在 Class.handleResponse (http://localhost:4200/assets/vendor.js:85238:14) 在 ajaxError (http://localhost:4200/assets/vendor.js:85736:25) 在 Class.hash.error (http://localhost:4200/assets/vendor.js:85310:23) 着火了 (http://localhost:4200/assets/vendor.js:3637:31) 在 Object.fireWith [as rejectWith] (http://localhost:4200/assets/vendor.js:3767:7) 完成后 (http://localhost:4200/assets/vendor.js:9576:14) 在 XMLHttpRequest。 (http://localhost:4200/assets/vendor.js:9816:9) defaultDispatch@vendor.js:27458

【问题讨论】:

  • 大多数模型文件名都是单数,所以将其更改为 customer 而不是 customers。在 createRecord 中提及模型名称为 customer 并确保对 /customers 的 POST 请求存在。

标签: ember.js ember.js-view


【解决方案1】:

Ember Data 似乎工作正常,但您的 API 服务器未运行或配置不正确。

newCustomer.save(); 尝试将记录保存到您的 API,错误解释了确切的问题:

Failed to load resource: the server responded with a status of 404
(Not  Found) vendor.js:27458 Ember Data Request POST /customers
returned a 404 Payload (text/html; charset=utf-8) Cannot POST /customers

尝试在您的 API 上将 POST 数据发送到 /customers 会导致服务器出现 404。

【讨论】:

    猜你喜欢
    • 2014-01-02
    • 1970-01-01
    • 2014-06-10
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 2015-03-20
    • 1970-01-01
    相关资源
    最近更新 更多