I am attempting to use the jQuery $.ajax call to return xml from a web service. If the call to this web service fails, for whatever reason, I have some static xml that i want to use. I am trying to use the timeout property to tell me if the call has failed or not and have put the xml parsing logic in my error function. I know the parsing of the xml works fine because I have tested it with the ajax call removed. When I add the ajax call with a dummy url to force an error, the $xml var never gets set. Any thoughts?
var navXml = '<?xml version="1.0" encoding="utf-8" ?><book><chapter id="1"></book>'; //static xml here
var serviceUrl = 'http://1234lkjasdf/test'; //bad service url to test failure
var $xml;
$.ajax({
type: 'GET',
dataType: 'xml',
url: serviceUrl,
timeout: 10,
success: function(data, status){
//set $xml var to xml data returned
$xml = data;
},
error: function (req, status, error){
//set $xml var to xml parsed from static value
xmlDoc = $.parseXML( navXml ),
$xml = $( xmlDoc );
}
});
EDIT
Looks like any error in $.ajax error handler silently fails. You have an error in your XML. Your chapter tag isn’t closed. It needs to be:
When I fix the error in XML, it works. Here’s a fiddle for that: http://jsfiddle.net/Pzdv5/1/
Your error handler needs to look like this:
You are missing var in front of
xmlDoc(will create a global variable), and more importantly, you have comma (,) instead of semicolon (;) after thexmlDocdeclaration.