Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/397.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

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
Javascript 如何使用主干将HTML表单中的数据添加到数据库中?_Javascript_Python_Backbone.js - Fatal编程技术网

Javascript 如何使用主干将HTML表单中的数据添加到数据库中?

Javascript 如何使用主干将HTML表单中的数据添加到数据库中?,javascript,python,backbone.js,Javascript,Python,Backbone.js,我有下一个问题-我需要将数据从HTML表单插入到我的服务器(Python)。 但似乎什么事都没有发生,也不起作用。 我做错了什么? 也许我的模特有问题 我的代码: 型号: 视图: 我的表单具有id“new_user”和与模型中相同的字段 UPD:谢谢大家!我知道怎么做了 视图的代码中有一些奇怪之处。在initialize()内部,您正在调用this.collection.on(),调用模型的save()函数。使用this.collection.on()时,需要传入对save函数本身的引用,而不是

我有下一个问题-我需要将数据从HTML表单插入到我的服务器(Python)。 但似乎什么事都没有发生,也不起作用。 我做错了什么? 也许我的模特有问题

我的代码: 型号:

视图:

我的表单具有id“new_user”和与模型中相同的字段


UPD:谢谢大家!我知道怎么做了

视图的代码中有一些奇怪之处。在
initialize()
内部,您正在调用
this.collection.on()
,调用模型的
save()
函数。使用
this.collection.on()
时,需要传入对
save
函数本身的引用,而不是对该函数的调用(
save()

我猜您的意思是调用视图上的
保存
函数,而不是模型。我在此处更新了代码以实现此目的:

define([
        "underscore",
        "backbone",
        "jquery",
        "text!pages/RestaurantPage/templates/AdminTemplate.html",
        "pages/RestaurantPage/models/User"

    ],

    function(_, Backbone, $, AdminTemplate, User) {
        return Backbone.View.extend({

            // ...

            initialize: function() {
                this.collection = new User();
                // Pass in a reference to the save function
                this.collection.on("change", this.save, this);
            },

            // ...
        });
    });

错误地,您定义了两次
事件
&彼此之间

有控制台错误吗?&将定义更改为仅包含一个事件定义,如下所示:

 return Backbone.View.extend({
            events: {
                'submit #new_user': 'save'
            },
            initialize: function() {
   //... remaining stuff goes here

真的,我不明白…我该怎么办?没有错误…什么都没发生。有时我会得到405错误-方法是不允许的
define([
        "underscore",
        "backbone",
        "jquery",
        "text!pages/RestaurantPage/templates/AdminTemplate.html",
        "pages/RestaurantPage/models/User"

    ],

    function(_, Backbone, $, AdminTemplate, User) {
        return Backbone.View.extend({

            // ...

            initialize: function() {
                this.collection = new User();
                // Pass in a reference to the save function
                this.collection.on("change", this.save, this);
            },

            // ...
        });
    });
 return Backbone.View.extend({
            events: {
                'submit #new_user': 'save'
            },
            initialize: function() {
   //... remaining stuff goes here