JQuery Ajax XML Return Values
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
1. <?xml version="1.0" encoding="utf-8"?>
2. <Mappings>
3. <Mapping DisplayName="Chinese" ShortName="zh" Code="4"/>
4. <Mapping DisplayName="Dutch" ShortName="nl" Code="19"/>
5. <Mapping DisplayName="English" ShortName="en" Code="9"/>
6. <Mapping DisplayName="Finnish" ShortName="fi" Code="11"/>
7. <Mapping DisplayName="French" ShortName="fr" Code="12"/>
8. <Mapping DisplayName="German" ShortName="de" Code="7"/>
9. <Mapping DisplayName="Italian" ShortName="it" Code="16"/>
10. <Mapping DisplayName="Japanese" ShortName="ja" Code="17"/>
11. <Mapping DisplayName="Russian" ShortName="ru" Code="25"/>
12. <Mapping DisplayName="Spanish" ShortName="es" Code="10"/>
13. </Mappings>
The Script:
1. function callerFunction(languageName) {
2. var languageCode = LANGUAGE.getLanguageCode(languageName);
3. alert(languageCode);
4. }
5.
6. LANGUAGE = {
7. getLanguageCode: function(languageShortName) {
8. $.ajax({
9. type: "GET",
10. url: "/languagemap.xml",
11. dataType: "xml",
12. success: function(xml) {
13. $(xml).find('Mapping').each(function(){
14. var displayName = $(this).attr('DisplayName');
15. var shortName = $(this).attr('ShortName');
16. var code = $(this).attr('Code');
17. if(shortName.toLowerCase() == languageShortName.toLowerCase()) {
18. alert(code);
19. return code;
20. }
21. });
22. },
23. error: function(){alert('error');}
24. });
25. },
26. getLanguageDisplayName: function(languageShortName) {
27. $.ajax({
28. type: "GET",
29. url: "/languagemap.xml",
30. dataType: "xml",
31. success: function(xml) {
32. $(xml).find('Mapping').each(function(){
33. var displayName = $(this).attr('DisplayName');
34. var shortName = $(this).attr('ShortName');
35. var code = $(this).attr('Code');
36. if(shortName.toLowerCase() == languageShortName.toLowerCase()) {
37. alert(displayName);
38. return displayName;
39. }
40. });
41. },
42. error: function(){alert('error');}
43. });
44. }
45. };
The alert in the success function(s) display the correct value, but the alert in the caller function yields undefined.
Lookig for suggestions
Primary Technology: JQuery
Primary Solution: Application Development