I'm using Backbone.js to structure an application that usually communicates with web services via JSON.
One web service will return JSON on success or XML in an error (clever, huh?). I need to parse this XML response to determine the error, but Backbone's JSON-centric thinking is causing me problems.
I have a collection that includes a parse function. The parse function is always called when the service returns JSON, and in this case I simply return the response object. However, when the service returns XML my fetch call's error callback function is called, and passed an error object with arguments[1] of parseerror. Further digging shows there was an unexpected < character.
Why is my parse function not being called to parse the XML before a parseerror is thrown? Furthermore - why, in the successful JSON calls, is it passed a JavaScript object (indicating that the JSON string has already been parsed)? Isn't the parse function supposed to do the parsing?
Relevant code below, any suggestions much appreciated.
var myCollection = Backbone.Collection.extend({
initialize : function() {
...
},
fetch: function(options) {
var options = {data: {...}, error: this.onFetchError};
Backbone.Collection.prototype.fetch.call(this, options);
},
onFetchError: function(arg1, arg2, arg3) {
debugger
},
parse: function(response) {
debugger
if(typeof response === 'object') {
return response;
}
}
});
return myCollection;
application/xml? And is the returned XML well-formed? - Brian Reischlapplication/xmland w3schools.com/xml/xml_validator.asp told me the XML was well-formed - tomfumbdataTypeoption on the request? Possibly it's being done in a$.ajaxSetup()or$.ajaxStart()call? If that's not it, I guess try setting breakpoints in the Backbonesuccessanderrorfunctions and see if that yields more information. - Brian Reischl