无法让Ajax POST与Emberjs一起工作
我现在要做的是尝试通过Ajax POST将数据发送到数据库。 我将首先从我的HTML表单开始,
接下来,是我的App.js
,根据Ember指南,我们必须首先映射模板吗? 所以这是我的router
:
App.Router.map(function() { this.resource('project'); });
接下来,我将数据插入的表格很简单,只有三个字段; id, projectname & projectdesc
。
App.Model = Ember.Object.extend({ }); App.Project = App.Model.extend({ id : null, projectname : null, projectdesc : null });
现在关于这个问题,
App.ProjectController = Ember.ArrayController.extend({ actions : { createNew : function() { this.get('model').createNew(); } } }); App.ProjectRoute = Ember.Route.extend({ }); App.Project.reopenClass({ createNew : function() { dataString = $("#project").serialize(); $.ajax({ type : "POST", url : "http://ankur.local/users/createNewProject", data : dataString, dataType : "json", success : function(data) { alert("yes"); } }); } });
我在控制台上收到此错误:
Uncaught TypeError: Object [object Array] has no method 'createNew'
这似乎就行了
this.get('model').createNew();
此外,我使用的是非RESTful PHP后端。
即使我认为我已经在模型中创建了该方法。 不过,我已经按照这种方式使用了GET并且它有效但不同之处在于我在模型中返回了该方法。 在这种情况下,我以为我必须从控制器调用该方法。 我可能做错了什么? 我将不胜感激任何帮助或建议。
代替
this.get('model').createNew();
试试下面的内容。
App.Project.createNew();