通过 AJAX 请求提交表单

发布于 2024-12-16 15:09:13 字数 688 浏览 0 评论 0原文

基于表单中输入的数据从 RESTful Web 服务检索数据的骨干惯例/最佳实践是什么?

据我了解,我可以在视图中注册一个点击观察者,因此当单击表单提交按钮时,视图将从表单中检索数据。但我不清楚我是否从视图模型路由器执行AJAX调用。

本质上,我不清楚以下哪一个是约定:

  1. 视图获取参数,执行AJAX调用,然后将返回的JSON传递给模型
  2. 视图获取参数并将它们传递给模型并让它执行AJAX调用并从Web服务检索数据
  3. 拥有视图获取参数并将它们传递给路由器并且它是否执行 AJAX 调用并相应地填充适当的模型和视图?

注意:我需要向 RESTful Web 服务提交搜索条件,该服务期望数据被 POSTed;如果我错了,请纠正我,但我似乎无法使用:fetchsave。我是否应该在模型中创建一个使用 $.ajax({...}) 来发布参数并接收 JSON 数据的函数?

What is backbone's convention/best practice for retrieving data from a RESTful web service, based on data entered in a form?

From what I understand I can register a click observer in the view, so when the form submit button is clicked the view will retrieve the data from the form. But I'm unclear on whether I execute the AJAX call from the view, model, or router.

Essentially, I'm unclear as to which of the following is convention:

  1. Have the view get the parameters, execute the AJAX call and then pass the returned JSON to the model
  2. Have the view get the parameters and pass them to the model and have it execute the AJAX call and retrieve the data from the web service
  3. Have the view get the parameters and pass them to a router and have it execute the AJAX call and populate the appropriate model and view accordingly?

NOTE: I need to submit search criteria to a RESTful Web Service, which expects the data to be POSTed; correct me if I'm wrong, but it doesn't seem like I can use: fetch or save. Should I create a function within the model that uses $.ajax({...}) to post the params and receive the JSON data?

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

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

发布评论

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

评论(2

青瓷清茶倾城歌 2024-12-23 15:09:14

将提交事件绑定到表单 id 或 div id,而不是按钮 id。

Bind the submit event to the form id or the div id, not the button id.

香橙ぽ 2024-12-23 15:09:13

backbone.js 模型已经设置为执行 RESTful 请求。方法2就是你想要做的。

您在模型中唯一需要设置的是 URL:

MyModel = Backbone.Model.extend({
    url: 'http://path/to/my/RESTful/service'
});

MyView = Backbone.View.extend({
   events: {
       'submit #myform': 'saveToModel'
   },

   initialize: function() {
       // ...
   },

   saveToModel: function() {
       // this triggers a RESTFul POST (or PUT) request to the URL specified in the model
       this.model.save({
          'foo': 'Foo!',
          'bar': 'Bar!'
       });
   }


});

The backbone.js models are already setup to do RESTful requests. Method 2 is what you want to do.

The only thing you have to setup in the model is a URL:

MyModel = Backbone.Model.extend({
    url: 'http://path/to/my/RESTful/service'
});

MyView = Backbone.View.extend({
   events: {
       'submit #myform': 'saveToModel'
   },

   initialize: function() {
       // ...
   },

   saveToModel: function() {
       // this triggers a RESTFul POST (or PUT) request to the URL specified in the model
       this.model.save({
          'foo': 'Foo!',
          'bar': 'Bar!'
       });
   }


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