开发者

Backbone.js getting response from server after a save()

开发者 https://www.devze.com 2023-04-03 08:41 出处:网络
I have a mode that saves data to a codeigniter function the codeigniter function returns valid JSON data back if it has an error how do i get the error details that the server returns when using save(

I have a mode that saves data to a codeigniter function the codeigniter function returns valid JSON data back if it has an error how do i get the error details that the server returns when using save(). I have the following code that doesn't work

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");
            }
        });

Success doesn't work at all


The 2nd option to save is an object with 2 properties, success and error. I'm assuming that you mean that "error" doesn't work at all, and success works fine, based on your actual question text.

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);}
    });

The error callback also passes model and response, so the response argument is what you're probably looking for.


The problem with your code is that you've got three hash arguments. The save method accepts attrs and options as its two arguments. As such, your code should look similar to this:

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');
  }
});

So your call would not have been able to attach the error function. The above example should work for you since it combines the success and error functions in the second hash argument.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号