Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you find the error generated by a backbone fetch

I'm new to backbone but I've written a basic model and when trying to fetch data for my model. I know the server is returning the data but fetch is calling the error callback.

That's fine but I don't know how I can find what error is being generated.

Here's the relevant code:

  mUser = Backbone.Model.extend({
    urlRoot: CURRENT_URL+'user',
    defaults: {
        name: '',
        age: 22,
        email: ''
    },
    initialize: function(){

    }
  });

  user = new mUser({'id':1});

  var x = user.fetch({
    error:   function(model, xhr, options){
       alert('Error on fetch')
       console.log(xhr.responseText);
    },
    success: function(model, response, options) {
        alert(user.toJSON());
    }
  })
  console.log('x email',x.email)

As I mentioned, the responseText does have the data I expect to see from the server, which is:

{'id':'1','name':'joe','age':'25','email':'[email protected]'}

Maybe I should mention that I'm, doing this as part of a PhoneGap android app. I don't think it's significant to the problem I'm having but it does limit my debugging options.

like image 745
dl__ Avatar asked Mar 12 '13 14:03

dl__


1 Answers

You are probably getting a parsererror when jQuery tries to parse the JSON response from your server. To check if you're getting a parsererror, add a complete callback and check the textStatus parameter. e.g.

user.fetch({
  complete: function(xhr, textStatus) {
    console.log(textStatus);
  }
});
like image 181
Ben Avatar answered Oct 25 '22 16:10

Ben