Avatar of brgdotnet
Flag for United States of America asked on

Need help reading XML using the browsers XMLHttpRequest object and client side JavaScript request

Can someone help me solve the following problem.  For the URL listed, assume that if you did go to that URL, then the XML will be  displayed in the web page, and it will look like the XML displayed in the "Code:" section of this post.
1. Create an HTML page with three <div>s and a button.
2.Upon clicking the button then using client-side JavaScript requests, get the XML returned from the URL:
using the browsers XMLHttpRequest object.
3.From the XML returned from the JavaScript request, display the EventTime 
and the Sport for the Sport with the latest post time in the three <div>s
created in the first step.
<?xml version="1.0" encoding="utf-8" ?> 
- <DATA EventTime="9:38:25 PM" ErrorText="" ErrorInt="0" ItemCount="122" >
  <EVENT Greek="Omega" Sport="SWIM" Date="3/2/2009" PostTime="3:25 PM" /> 
  <EVENT Greek="Omega" Sport="RUN"  Date="3/2/2009" PostTime="8:55 PM" /> 
  <EVENT Greek="Omega" Sport="SKI"  Date="3/2/2009" PostTime="4:51 PM" />  
  <EVENT Greek="Omega" Sport="SURF" Date="3/2/2009" PostTime="8:55 PM" /> 
  <EVENT Greek="Omega" Sport="BOWL" Date="3/2/2009" PostTime="5:50 PM"/> 
  <EVENT Greek="Omega" Sport="CYCLE"  Date="3/2/2009" PostTime="7:55 PM"/> 
  <EVENT Greek="Omega" Sport="FISH" Date="3/2/2009" PostTime="2:45 PM"/> 
  <EVENT Greek="Omega" Sport="SKEET" Date="3/2/2009" PostTime="1:55 PM"/> 
  <EVENT Greek="Omega" Sport="SOCCER" Date="3/2/2009" PostTime="3355 PM"/> 

Open in new window


Avatar of undefined
Last Comment

8/22/2022 - Mon
Pieter Marais

View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.

I appreciate your help. I noticed it was not working and so I traced into the code in the debugger. I get an "Access Denied" message when this line of code is executed:

    self.xmlHttpReq.open('POST', strURL, true);

 That line of code is inside of the function xmlhttpPost(strURL, strQueryString)

Do you have any idea why I am getting that message?
Pieter Marais

AJAX has this built-in security functionality that prevents you from making requests to a domain other than that on which it is hosted. So if this script, for example, is hosted on www.myajax.com, you can't go and make requests to www.wireless.org. Hence the "Access Denied" message.

If you want to use this script, the file or script that you wish to access with the AJAX must be on the same domain.

There are workarounds though, depending on which system you are using the AJAX. For example, if your system supports PERL, you can use a script that implements the HTTP::Request and LWP::UserAgent modules.

So instead of calling the URL directly, you can use a script on your server that retrieves the information for you for the other domain and then returns it to the AJAX on your domain.

Thanks Buddy!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes