Click here to Skip to main content
15,886,789 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys!
Trying to parse an xml using jquery.
This seems to work only With IE 11 (didn't try older releases). Chrome, Opera and Firefox don't do anything..
<script type="text/javascript">
      if (window.XMLHttpRequest)
      {
        xmlHttp = new XMLHttpRequest();
      }
      else
      {
        xmlHttp = new ActiveXObject("Microsoft.XmlHTTP");
      }
      xmlHttp.open("GET", "DataSources/XML/XMLCountries.XML", false);
      xmlHttp.send();
      xmlDoc = xmlHttp.responseXML;
      var $xml = $(xmlDoc);
      $xml.find('Country').each(function () {
        alert($(this).attr('Name'));
      });
    </script>

This is the XML file:
XML
<?xml version="1.0" encoding="utf-8"?>
<CountriesList xmlns="XMLCountries.xsd">
  <Country Name="England" NavigateUrl="" ImageUrl="~/Images/flags/shiny/16/England.png" Tooltip="English Leagues and Cups"/>
  <Country Name="France" NavigateUrl="" ImageUrl="~/Images/flags/shiny/16/France.png" Tooltip="French Leagues and Cups"/>
  <Country Name="Germany" NavigateUrl="" ImageUrl="~/Images/flags/shiny/16/Germany.png" Tooltip="German Leagues and Cups"/>
  <Country Name="Italy" NavigateUrl="" ImageUrl="~/Images/flags/shiny/16/Italy.png" Tooltip="German Leagues and Cups"/>
  <Country Name="Spain" NavigateUrl="" ImageUrl="~/Images/flags/shiny/16/Spain.png" Tooltip="Spanish Leagues and Cups"/>
  <Country Name="Portugal" NavigateUrl="" ImageUrl="~/Images/flags/shiny/16/Portugal.png" Tooltip="Portugal Leagues and Cups"/>
</CountriesList>

What am I missing?? I'm quite sure IE 11 uses XMLHttprequest in place of Microsoft.XMLHttp, so this shouldn't be an issue of the XMLHttprequest.
Thx for your help!
Posted
Updated 12-May-14 21:35pm
v4
Comments
Matej Hlatky 13-May-14 5:21am    
The issue you are facing occurs when you are performing the Ajax request or when parsing the given XML document.
Check the developer console for errors.
Also, it seems you are using jQuery, so why not to use jQuery.get ?
leotato008 13-May-14 6:00am    
Got the error (I'm not very familiar with js debugging, so it took a while..)
This is what I receive!

XMLHttpRequest cannot load http://localhost/FTBL/FTBL/DataSources/XML/XMLCountries.XML. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:49373' is therefore not allowed access.

I'm reading something about CORS, I've added a couple of Headers:
Access-Control-Allow-Origin,Headers and Methods, but no luck anyway..

1 solution

Gotcha!
Added these lines inside the web.config
XML
<add name="Access-Control-Allow-Origin" value="*" />
<add name="Access-Control-Allow-Methods" value="*" />
<add name="Access-Control-Allow-Headers" value="*" />

I had trouble understanding where I had to put these lines.
Thx Matej!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900