【问题标题】:POST to server with custom URL?使用自定义 URL 发布到服务器?
【发布时间】:2014-04-08 17:13:46
【问题描述】:

我正在尝试连接我的服务器以传递 JSON 格式的 POST 请求。

但是,我将其发送到的 URL 取决于在模板上查看的 id。因此,如果我正在查看 ID 为 1100 的“组”,则 URL 应该是:

http://localhost:3100/api/groups/1100/something/start.

我只是不知道如何在其中获取组 ID。尝试,但没有骰子(好吧,因为我不知道我应该如何正确定义params

jQuery.ajax({
         url: "/api/groups/"+ params.group_id +"/something/start",
         type: 'post',
             contentType: 'application/json',
             dataType: 'json'
                  });
                  },

一些更有用的信息:

我的路线:

App.IndexRoute = Ember.Route.extend({                                                                                  
 sortProperties: ['id'],                                                                                              
 sortAscending: true,                                                                                                 

  actions: {                                                                                                           
toggleMenu: function() {                                                                                           
  this.controller.toggleProperty('menuVisible');                                                                   
  this.controller.pushBody();                                                                                      
}                                                                                                                  
  },                                                                                                                   

  model: function() {                                                                                                  
var store = this.store;                                                                                            
var url = '/api/groups/top?subscribe=true';                                                   
$.getJSON(url).then(function(data) {                                                                               
  store.pushMany('group', data.data.groups); 


});
 return this.store.all('group');                                                                                                                
  }

});     

控制器:

App.IndexController = Em.ArrayController.extend({
 sortProperties: ['name'],
sortAscending: true,
 menuVisible: false,     
 actions: {
 poweron: function(){
         jQuery.ajax({
             url: "/api/groups/"+ params.group_id +"/something/start",
             type: 'post',
                 contentType: 'application/json',
                 dataType: 'json'
                      });
                      }

});

想法?谢谢!

【问题讨论】:

  • 如果 params 是一个具有 group_id 键值的对象,那么你所拥有的应该可以正常工作。
  • 我已经在上面添加了控制器和路由。仍然得到params is not defined 。谢谢!
  • 好吧,你定义params了吗?为什么需要params?为什么你不能直接通过goup_id
  • 我认为这是我的问题 - 我不熟悉如何做到这一点,并且没有运气搜索。也许我不需要params - 我只是认为这就是它的完成方式。在那里弹出group_id 给我同样的,group_id is not defined
  • 您的 group_id 存储在哪里?你怎么知道使用哪个 group_id?

标签: jquery ember.js ember-data


【解决方案1】:

原来我需要将 id 参数传递给控制器​​ - 控制器怎么知道要使用哪个 id

更多 - http://emberjs.com/guides/templates/actions/#toc_action-parameters

正确的动作看起来更像这样:

actions: {
poweron: function(id){
       group_id = id;
       alert(group_id); //let's see what's being sent over

         jQuery.ajax({
             url: "/api/groups/"+ group_id +"/something/start",
             type: 'post',

 ........etc and so on.

【讨论】:

  • 不要忘记group_id = id 之前的var 关键字。或者更好的是,接受你的 id 作为 group_id poweron: function(group_id){ 这样你就不会创建额外的 var
猜你喜欢
  • 2011-03-14
  • 1970-01-01
  • 2014-07-25
  • 1970-01-01
  • 2020-11-18
  • 2010-11-28
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多