【问题标题】:call service rest on click with Backbone使用 Backbone 调用服务休息
【发布时间】:2014-01-22 15:02:56
【问题描述】:

当我按下按钮登录时,我想调用一个休息服务(帖子),但它没有启动任何服务,它只是添加了一个“?”在我的应用程序的网址末尾。

这是我的 js:

(function ($) {
var authentication = Backbone.Model.extend({
     defaults: {
            Username: "",
            Password: ""
        },
        url:'../../rest/login'
});



var LoginView = Backbone.View.extend({
    model: new authentication(),
    el: $("#login-form"),
    events: {
        "click button#login": "login"
    },

    login: function(){
        alert("ici");
        this.model.save({username: this.$el.find("#inUser")}, {
        password: this.$el.find("#inPswd")}, {
            success: function() {
                /* update the view now */
            },
            error: function() {
                /* handle the error code here */
            }
        });
    }   

})
})
(jQuery);

这是我的表格:

<form class="form-inline">
<div class="form-group">
<input type="text" class="form-control" placeholder="Username" id="inUser"></input>
<input type="password" class="form-control" placeholder="Password" id="inPswd"></input>
<button id="login">Login</button>
</div>
</form>

【问题讨论】:

    标签: javascript rest backbone.js


    【解决方案1】:

    您的.save() 方法调用有问题,因为您在两个不同的对象中发送usernamepassword

    还要停止添加问号 ? 符号(停止提交表单),您需要将 event.preventDefault(); 和/或 return false; 添加到按钮单击处理程序。

    这是一个修复:

    login: function(event) {
        event.preventDefault();
        alert("ici");
        this.model.save({
                username: this.$el.find("#inUser"),
                password: this.$el.find("#inPswd")
            }, {
                success: function() {
                    /* update the view now */
                },
                error: function() {
                    /* handle the error code here */
                }
        });
        return false;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-31
      • 1970-01-01
      • 2016-04-02
      相关资源
      最近更新 更多