如何在ember.js中使用ajax发送Post请求?

时间:2014-06-20 12:17:42

标签: forms ember.js login

我想使用ember.js向服务器发送POST(非GET)请求。我不知道“这里有哪个功能”需要哪个功能,但我想将它发送到服务器以获取登录请求。

App.LoginController = Ember.ObjectController.extend({
  actions: {
    userLogin: function(user) {
      // which function here? 
      ?? ("http://siteurl/api/authentication/login/&username=" + user.username + "&password=" + user.password + ""); 
      this.transitionTo('cat');

    },

    cancelLogin: function() {
      this.transitionTo('menu');
    }
  }
});

App.UserFormComponent = Ember.Component.extend({
  actions: {
    submit: function() {
      this.sendAction('submit', {
        username: this.get('username'),
        password: this.get('password')
      });
    },

    cancel: function() {
      this.sendAction('cancel');
    }
  }
});

在这里下载模板代码

  <script type="text/x-handlebars" data-template-name="login">
        <header class="bar bar-nav">
            <h1 class="title">inloggen</h1>
            {{#link-to 'menu' class="icon icon icon-bars pull-right"}}{{/link-to}}
        </header>   
        <!-- SHOW LOADER -->
        <div class="content">  
        <div class="content-padded">
        {{user-form submit="userLogin" cancel="cancelLogin" submitTitle="login"}}
        </div>
  </script>

  <script type="text/x-handlebars" data-template-name="components/user-form">
   <form {{action "submit" on="submit"}}>
     <p><label>gebruikersnaam {{input type="text" value=username}}</label></p>
     <p><label>wachtwoord {{input type="password" value=password}}</label></p>
     <input type="submit" class="btn btn-primary btn-block" {{bindAttr value=submitTitle}}>
     <button class="btn btn-negative btn-block" {{action "cancel"}}>Cancel</button>
   </form>
  </script>

1 个答案:

答案 0 :(得分:9)

Ember没有任何内置的通信层,你可以使用jquery进行此类调用。

App.LoginController = Ember.ObjectController.extend({
  actions: {
    userLogin: function(user) {      
      $.ajax({
        type: "POST",
        url: "http://siteurl/api/authentication/login/&username=" + user.username + "&password=" + user.password,
        data: { name: "John", location: "Boston" }
      })
      this.transitionTo('cat');

    },

    cancelLogin: function() {
      this.transitionTo('menu');
    }
  }
});