[jQuery] Returning an XML attribute from success or complete
I am trying to create a function where i can return some values from
an XML file with the $Ajax call.
The XML:
located in the root of my webb application /languagemap.xml
<?xml version="1.0" encoding="utf-8"?>
<Mappings>
<Mapping DisplayName="Chinese" ShortName="zh" Code="4"/>
<Mapping DisplayName="Dutch" ShortName="nl" Code="19"/>
<Mapping DisplayName="English" ShortName="en" Code="9"/>
<Mapping DisplayName="Finnish" ShortName="fi" Code="11"/>
<Mapping DisplayName="French" ShortName="fr" Code="12"/>
<Mapping DisplayName="German" ShortName="de" Code="7"/>
<Mapping DisplayName="Italian" ShortName="it" Code="16"/>
<Mapping DisplayName="Japanese" ShortName="ja" Code="17"/>
<Mapping DisplayName="Russian" ShortName="ru" Code="25"/>
<Mapping DisplayName="Spanish" ShortName="es" Code="10"/>
</Mappings>
The Script:
function callerFunction(languageName) {
var languageCode = LANGUAGE.getLanguageCode(languageName);
alert(languageCode);
}
LANGUAGE = {
getLanguageCode: function(languageShortName) {
$.ajax({
type: "GET",
url: "/languagemap.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('Mapping').each(function(){
var displayName = $(this).attr('DisplayName');
var shortName = $(this).attr('ShortName');
var code = $(this).attr('Code');
if(shortName.toLowerCase() == languageShortName.toLowerCase()) {
alert(code);
return code;
}
});
},
error: function(){alert('error');}
});
},
getLanguageDisplayName: function(languageShortName) {
$.ajax({
type: "GET",
url: "/languagemap.xml",
dataType: "xml",
success: function(xml) {
$(xml).find('Mapping').each(function(){
var displayName = $(this).attr('DisplayName');
var shortName = $(this).attr('ShortName');
var code = $(this).attr('Code');
if(shortName.toLowerCase() == languageShortName.toLowerCase()) {
alert(displayName);
return displayName;
}
});
},
error: function(){alert('error');}
});
}
};
the alert in the success function(s) display the correct value, but
the alert in the caller function yields undefined.
Lookig for suggestions