I am having a service point implemented with ASP.NET web API. It returns a list, an IEnumerable of dto’s embedded within HttpResponseMessage.
However my client code which is implemented via backbone collection fetch always end up with error. Inside the error method, while I pass & watch the parameter I see the collection is appears under ResponseText property.
While I directly pass IEnumerable<dto> without wrapping it with HttpResponseMessage, it works fine.
My question: What is the correct way? How can I make my backbone collection refer to what inside ResponseText property?
I try to play around to make a single model fetch, and override fetch method.
categories = new Categories({ url: 'http://localhost:6685/category' });
$.when(categories.fetch())
.then(
function() {
app.init({ 'categories': categories, 'cart': cart });
});
This keep giving me ambiguous errors. then when i switched with
categories.fetch({
success: function() {... },
error: function(err) {
console.log(err);
}
});
I can see inside err my categories list. the property name is responsetext.
var Categories = Backbone.Collection.extend({
model: Category,
initialize: function (options) {
this.url = options.url;
},
parse:function (response) {
return response.categories;
},
Server-side
HttpResponseMessage<IEnumerable<category>> httpresponse = null;
var response= _catalogueservice.getcategories();
if (response.success)
{
httpresponse = new HttpResponseMessage<IEnumerable<category>>(response.categories, response.categories.Count()>0?HttpStatusCode.Found:HttpStatusCode.NotFound);
}
else
{
httpresponse = new HttpResponseMessage<IEnumerable<category>>( HttpStatusCode.NotFound);
}
return httpresponse;
I can see under fiddler that JSON is being returned.
Always keep your services Dry simple. You are into nightmare when you find out most of the api users have to use another wrapper around data just because of your convenience. Anyways keep that aside below is all that you have to do
taking into account your response looks like below