javascript - Backbone JS Model Fetch returns 200 in Fiddler but invokes error method -
i have weird stuff going on here. have model i'm attempting fetch on. can @ calls in fiddler or call api service manually , json expect back, fetch method throws error in backbone. not tell me why , responsetext empty. in fiddler, service returns 200.
here's calling code...
this.createsession = function (sessionid) { var = this; cookiemanager.createsession(sessionid); var sessiondata = new authentication.response({ id: sessionid }); sessiondata.fetch({ success: function () { that.storedata(sessiondata); }, error: function (model, xhr) { if (console !== undefined && console !== null) { console.log('unable load session data: ' + xhr.responsetext); } throw new error('unable load session data'); } }); };
and model concerned...
define(['ministry', 'moment'], function (ministry) { var authrequest = ministry.model.extend({ name: 'auth request', urlroot: '/api/auth', defaults: { id: undefined, requestsource: undefined, apikey: undefined, username: undefined, password: undefined, datecreated: undefined, lastloggedin: undefined }, generatedhash: undefined, parse: function(attributes, response) { var hashurl = response.xhr.getresponseheader('location'); var elements = hashurl.split("/"); this.generatedhash = attributes.id = elements[elements.length - 1].tolowercase(); return attributes; }, validate: function (attributes) { if (attributes.requestsource === undefined || attributes.requestsource == null || attributes.requestsource == '') { return "the attribute 'requestsource' required"; } if (attributes.apikey === undefined || attributes.apikey == null || attributes.apikey == '') { return "the attribute 'apikey' required"; } if (attributes.username === undefined || attributes.username == null || attributes.username == '') { return "the attribute 'username' required"; } if (attributes.password === undefined || attributes.password == null || attributes.password == '') { return "the attribute 'password' required"; } return null; } }); // return 2 types syntactic sugar reasons. return { request: authrequest, response: authrequest }; });
the model extends common root model, code here...
var ministrymodel = backbone.model.extend({ name: 'unnamed model', initialize: function (options) { this.options = options || {}; backbone.model.prototype.initialize.call(this, options); if (this.options.name) { this.name = this.options.name; } this.on('invalid', function (model, error) { if (console !== undefined) { console.log(error); console.log(model); } throw (new error(error)); }); }, fetch: function (options) { this.trigger('fetching', this, options); return backbone.model.prototype.fetch.call(this, options); } });
the error callback being called , can't figure out why. in fiddler following calling api directly...
http/1.1 200 ok cache-control: no-cache pragma: no-cache content-type: application/json; charset=utf-8 expires: -1 server: microsoft-iis/8.0 x-aspnet-version: 4.0.30319 x-powered-by: asp.net date: mon, 19 aug 2013 16:12:00 gmt content-length: 213 {"id":"bba5e742d1af3de5921e8df0a1818530789c202a","datecreated":"2013-07-22t17:57:20.143","lastloggedin":"2013-08-19t17:08:29.67","username":"tiefling","apikey":"ka73nd9s7n2ls9f3ka2n5p2k","requestsource":"website"}
and when interrogating call chrome...
http/1.1 200 ok cache-control: no-cache pragma: no-cache content-type: application/json; charset=utf-8 expires: -1 server: microsoft-iis/8.0 x-aspnet-version: 4.0.30319 x-powered-by: asp.net date: mon, 19 aug 2013 16:08:29 gmt content-length: 213 {"id":"bba5e742d1af3de5921e8df0a1818530789c202a","datecreated":"2013-07-22t17:57:20.143","lastloggedin":"2013-08-19t17:08:29.67","username":"tiefling","apikey":"ka73nd9s7n2ls9f3ka2n5p2k","requestsource":"website"}
both show 200 show different icon in fiddler - chrome call icon red circle, other looks hunky dory.
any ideas welcome, i've hit bit of brick wall 1 now.
additional: if step through code, when fetch call, attributes within 'sessiondata' object haven't been updated @ all. object same before fetch method executed.
update: since first wrote this, code has stopped making call @ far can see fiddler. working earlier, seems failing invisibly no indication of error be. debugging api code indicates no call being made @ method on service.
further update: without changing code except removing error / success callbacks, started working (kind of). if put breakpoint in after fetch call works (from can see in fiddler - can't check app success callback unwired). if take out breakpoint partially fails (red circle in fiddler 200 response).
this seems kind of race condition?
i've tried getting info using 'complete' callback use textstatus, says 'error' - not particularly useful!
i found answer this; isn't obvious in above code. calling code being called higher method this...
var authdata = siansplanapp.session.getdata(this.$loginusername.val(), this.$loginpassword.val()); authdata.save(authdata.attributes, { success: function() { if (authdata.generatedhash !== undefined && authdata.generatedhash !== null && authdata.generatedhash !== '') myapp.session.createsession(authdata.generatedhash); that.redirecttoorigin(); $.fancybox.close(); }, error: function (model, xhr) { if (console !== undefined && console !== null) { console.log('login attempt failed: ' + xhr.responsetext); } that.displayerror('username or password not recognised'); $.fancybox.close(); } });
as can see, createsession method followed call redirecttoorigin() - although not shown, suffice method redirects page root. reason fails simple - initial create call run through redirect occurs there never time callbacks made.
Comments
Post a Comment