Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Forms 如何在ember.js中使用ajax发送Post请求?_Forms_Ember.js_Login - Fatal编程技术网

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

Forms 如何在ember.js中使用ajax发送Post请求?,forms,ember.js,login,Forms,Ember.js,Login,我想用ember.js向服务器发送一个POST(not GET)请求。我不知道我在“此处的哪个函数”需要哪个函数,但我想将它发送到服务器以请求登录 App.LoginController = Ember.ObjectController.extend({ actions: { userLogin: function(user) { // which function here? ?? ("http://siteurl/api/authentication/l

我想用ember.js向服务器发送一个POST(not 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>

因洛根
{{{#链接到'menu'class=“icon-icon-icon-bars-pull right”}{{{/link-to}}
{{user form submit=“userLogin”cancel=“cancelogin”submititle=“login”}
gebruikersnaam{{input type=“text”value=username}

wachtwoord{{input type=“password”value=password}}

取消
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');
    }
  }
});

现在最好使用ember ajax。是的,ember ajax是一个更好的解决方案:在这里:最好现在使用ember ajax定义“更好”?ember ajax页面上没有真正提到的内容,但可能正确的是,ember ajax与ember run循环一起使用可能更安全。诚然,我的超旧答案仍然是“正确的”,因为没有内置的通信层,但社区等推动了ember数据的发展,尽管我经常发现,在没有大型数据生态系统的情况下(当然,为什么不使用ember ajax),只使用简单的ajax调用更容易。