主干通过 AJAX 向服务器发送参数

发布于 2024-11-07 15:05:05 字数 727 浏览 0 评论 0原文

嘿, 我对骨干网很陌生,我读了一些文档,但没有找到合适的东西。我想使用骨干网通过 AJAX 通信将一些数据发送到服务器。骨干有某种捷径可以做到这一点吗?现在,我的简单示例由一个 HTML 页面组成,其中包含一个用户名输入和一个密码输入。单击按钮后,我会触发此功能:

    //handling the button event on the login form
    function loginPressed(){
        var username = document.getElementById("username").value;
        var password = document.getElementById("password").value;

        loginModel.username = username;
        loginModel.password = password;


    }

登录模型只是我用主干创建的模型:

//creating a backbone model to handle login
var loginModel = new Backbone.Model({
        username: "",
        password: "", 
    });

您认为我可以用它向我的服务器发出 AJAX 请求吗?

谢谢, 马西亚尔

​我的服务器端是node.js

Hey,
I'm very new to backbone, and I've read a little bit of the documentation but I didn't find something appropriate. I would like to use backbone to send some data to the server using AJAX communication. Does backbone have some kind of shortcut to do this? For now my simple example consist of a HTML page with an input for username and one for the password. On clicking the button I fire this function:

    //handling the button event on the login form
    function loginPressed(){
        var username = document.getElementById("username").value;
        var password = document.getElementById("password").value;

        loginModel.username = username;
        loginModel.password = password;


    }

The login model is just a model I've created with backbone:

//creating a backbone model to handle login
var loginModel = new Backbone.Model({
        username: "",
        password: "", 
    });

Do you think I can do AJAX request to my server with this?

Thanks,
Masiar

P.S. My serverside is node.js

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷…雨湿花 2024-11-14 15:05:05

您的模型定义不正确:

var LoginModel = Backbone.Model.extend({
    url: "/your/login/url"
});
...

function loginPressed(){
  var login = new LoginModel
  login.save({
    username: document.getElementById("username").value,
    password: document.getElementById("password").value
  },{
    /*any other jquery ajax options including callbacks*/
  });
}

有一些原因您不应该使用 ajax 调用登录,但您很快就会学到它们:)

Your model is not defined properly:

var LoginModel = Backbone.Model.extend({
    url: "/your/login/url"
});
...

function loginPressed(){
  var login = new LoginModel
  login.save({
    username: document.getElementById("username").value,
    password: document.getElementById("password").value
  },{
    /*any other jquery ajax options including callbacks*/
  });
}

There are some reasons you should not do login with ajax calls but you'll learn them soon enough :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文