如何使用 backbone.js 的 'save' 方法 POST 数据

How to POST data using the 'save' method of the backbone.js

我正在尝试使用 backbone 'save' 方法保存表单数据。我正在使用 POST http 方法将数据保存到服务器。数据确实保存到服务器,但是在网络控制台中,我看到 POST 状态显示已取消,但已成功创建数据。 这是 js:

var account = Backbone.View.extend({
    el: "#account",
    events:{
        'click button#save' : 'saveList'
    },
    render: function(id){
        value = new accountModel([],{id:id});
    },              
    saveList: function(event){  
        var values = $('#form').serializeJSON();        
            value.save(values, {
                success: function(value){
                    console.log(“success”)  
                },
                error: function(err){
                    console.log('sorry, your record was not saved' + err);  
                }
            });
});

html:

<form class="form" id="form">
    <table>
        <tbody>
            <tr>
             <label>name</label>
             <td><input type="text" name="accountName" maxlength="50"/> </td>            
           </tr>
           <tr>
             <label>first</label>
             <td><input type="text" name="first" maxlength="50"/> </td>            
           </tr>
          <tr>
             <label>last</label>
             <td><input type="text" name="last" maxlength="50"/> </td>            
           </tr>
      </tbody>
   </table>
</form>

我在 success() 和 error() 上设置了一个调试器点,它总是出错,但仍然发布数据。我在这里做什么,为什么它表现得如此奇怪?有什么想法吗??

看起来这里有答案:jquery $.post() getting canceled

基本上,您需要在 saveList 函数中添加一个 event.preventDefault()(在其他任何事情之前)。