user1234
user1234

Reputation: 3159

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

I;m trying to save a form data using the backbone 'save' method. I'm using the POST http method for saving the data to the server. The data does get saved to the server, however in the network console, i see POST status shows cancelled, but successfully creates the data. here is the 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>

I set a debugger point at the success() and the error(), it always goes to the error, but still posts the data..What im I doing here and why is it behaving such weirdly? Any ideas??

Upvotes: 0

Views: 198

Answers (1)

olan
olan

Reputation: 3648

Looks like this is answered here: jquery $.post() getting canceled

Basically, you need an event.preventDefault() in the saveList function (before anything else).

Upvotes: 2

Related Questions