2011-09-05 35 views
0

我有一個模式,將數據保存到codeigniter函數codeigniter函數返回有效的JSON數據返回,如果它有一個錯誤如何獲取服務器返回的錯誤詳細信息使用save()時。我有下面的代碼不起作用Backbone.js從保存後得到服務器的響應()

this.newproject.save({ 
      'Objective':Objective, 
      "Planner":Planner, 
      "NISupervisor":NISupervisor, 
      "SiteIDs":SiteIDs, 
      "Status":Status , 
      "StartDate":StartDate, 
      "EndDate":EndDate, 
      "Details":Details, 
      "PrjTitle":PrjTitle 
     },{ 
      success:function(model,response){ 
       console.log(response); 
      } 
     },{ 
      error:function(){ 
       alert("wrong"); 
      } 
     }); 

成功並不在所有

回答

3

第二選擇工作,以節省與2個屬性,成功和錯誤的對象。我假設你的意思是「錯誤」根本不起作用,並且成功的正常工作,基於你的實際問題文本。

this.newproject.save({ 
     'Objective':Objective, 
     "Planner":Planner, 
     "NISupervisor":NISupervisor, 
     "SiteIDs":SiteIDs, 
     "Status":Status , 
     "StartDate":StartDate, 
     "EndDate":EndDate, 
     "Details":Details, 
     "PrjTitle":PrjTitle 
    },{ 
     success:function(model,response){console.log(response);}, 
     error:function(model,response){console.log(response);} 
    }); 

錯誤回調還傳遞模型和響應,所以響應參數就是您可能要查找的內容。

1

你的代碼的問題是你有三個散列參數。 save方法接受attrs和options作爲它的兩個參數。因此,您的代碼應該類似於此:

var attrs = { 
    "Objective":Objective, 
    "Planner":Planner, 
    "NISupervisor":NISupervisor, 
    "SiteIDs":SiteIDs, 
    "Status":Status , 
    "StartDate":StartDate, 
    "EndDate":EndDate, 
    "Details":Details, 
    "PrjTitle":PrjTitle 
}; 
this.newproject.save(attrs, { 
    success: function(model, response) { 
    console.log(response); 
    }, 
    error: function(model, response) { 
    alert('wrong'); 
    } 
}); 

所以你的電話不會已經能夠附加誤差函數。上面的例子應該適合你,因爲它將第二個散列參數中的成功和錯誤函數結合在一起。

相關問題