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.
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);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With