2012-06-18 46 views
2

我有一个RESTful服务,当我尝试保存新记录时,会执行唯一的电子邮件检查。当它失败时,我返回一个400错误代码以及带有“错误”值的JSON消息。未在400 HTTP上调用错误回调返回

不幸的是,我保存的错误回调似乎没有被解雇。有什么建议么?

var nRecord = new RecordModel(values); 
nRecord.save({ 
    wait: true, 
    error: function(model,response){ 
     console.log('error2'); 
     obj.errorHandler(model,response); 
    }, 
    success: function() { 
     console.log('success2'); 
     obj.alert('Information saved!','Record Created!','success'); 

     // if there were no errors, clear the list 
     $('#record-form :input').val(''); 
    } 
}); 

“Error2”从不显示在控制台中。思考?

回答

8

您似乎将您的错误处理程序和其他选项作为模型属性传递并保存到模型中。尝试将null作为第一个参数传递给保存函数。希望这将使它工作:)

nRecord.save(null,{ 
    wait: true, 
    error: function(model,response){ 
     console.log('error2'); 
     obj.errorHandler(model,response); 
    }, 
    success: function() { 
     console.log('success2'); 
     obj.alert('Information saved!','Record Created!','success'); 

     // if there were no errors, clear the list 
     $('#record-form :input').val(''); 
    } 
}); 
+1

做到了!去图....感谢修复 – enygma

+0

嗯......我有同样的问题,但与'fetch()',而不是'save()'。这个解决方案不适用于我(主干0.9.2)。我尝试使用'(originalModel,resp,options)'参数,如onError回调的源代码中所示。我不明白... –

+0

这个解决方案对我来说也不适用。什么是公认的方法? – 2012-12-04 22:25:31

1

我有这个问题,这个问题是我是覆盖在模型同步。这意味着选项没有得到通过。

completeCustomer.save(null, { 
       wait: true, 
       error: function(model,response){ 
        console.log('error2'); 
        //obj.errorHandler(model,response); 
        _.bind(this.handleError, this); 
       }, 
       success: function() { 
        console.log('success2'); 
        //_.bind(this.handleSuccess, this); 
       } 
      });