0

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??

user1234
  • 3,000
  • 4
  • 50
  • 102
  • What does the error look like? What's the response or error code? – Cory Danielson Apr 09 '15 at 22:31
  • 1
    You're also instantiating your account model awkwardly. `new accountModel([],{id:id});` The proper way to initialize a model is with the `attributes` first followed by the `options` and the `attributes` should be an object, not an array. I'm guessing that your `url` function inside the model relies on `this.id` ? That's not really the proper way to store an ID on the model.. it should be in the attributes. – Cory Danielson Apr 09 '15 at 22:33

1 Answers1

2

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

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

Community
  • 1
  • 1
olan
  • 3,568
  • 5
  • 24
  • 30