2012-06-18 45 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); 
       } 
      });