Recent Widgets


Register for DashboardWidgets

Recent Forums Posts

Partners


iCompositions

MacDesktops.net

RSS Showcase
RSS Comments
RSS Forums

This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies. Posted in: Widget Central

XMLHTTPREQUEST problem!

Author Message
TomGillespie



Joined: 23 Jul 2005
Posts: 3

Posted: Sat Jul 23, 2005 - 6:54 pm    Post subject: XMLHTTPREQUEST problem! Reply with quote

Sorry, this is a real newbie problem, but its causing me a headache already.

I've tried looking at other posts and examples but with no prevail.

Code:
function setup()
{
   var req = new XMLHttpRequest();

url = "http://www.adomain.com/afile.php";
req.open("GET", url ,false);
req.send(null);
       
//req.responseText returns a string
response = req.responseText;

document.getElementById('content').innerHTML = response;
// or .innerText
}


I'm attempting to use this code to print the contents of afile.php to the widget. In the future, afile will be a proper PHP page, but for now it simply contains the character '1'.

The function here, setup() is triggered when the body of my widget's html file loads, but all that happens is that after a second or so the background to my widget disappears, with no sign of the output from the php file.

What am I doing wrong?!

Thanks a lot!
View user's profile Send private message Send e-mail Widgets
gnome
Administrator


Joined: 26 Sep 2004
Posts: 145

Posted: Sat Jul 23, 2005 - 8:54 pm    Post subject: Reply with quote

You're using XmlHttpRequest in Async mode, but expecting the results immediately. Async mode isn't going to wait for the server to load, it's just going to keep on executing code.The solution is to follow the Apple examples available here, and especially note the onreadystatechange handler function that async XmlHttpRequest uses to say "I'm done loading, you can process now."

...or, much simpler, change the third argument of req.open() to true. That takes you out of Async mode and your program will hang while the page loads, stalling the code execution and therefore delaying your call to the response HTML. The problem is that if the request fails, so does your widget. Only use it on reliable/fast servers.
View user's profile Send private message Widgets
Chris



Joined: 27 Jan 2005
Posts: 344
Location: Durham, UK

Posted: Sun Jul 24, 2005 - 4:29 am    Post subject: Reply with quote

gnome wrote:
You're using XmlHttpRequest in Async mode, but expecting the results immediately. Async mode isn't going to wait for the server to load, it's just going to keep on executing code.The solution is to follow the Apple examples available here, and especially note the onreadystatechange handler function that async XmlHttpRequest uses to say "I'm done loading, you can process now."

...or, much simpler, change the third argument of req.open() to true. That takes you out of Async mode and your program will hang while the page loads, stalling the code execution and therefore delaying your call to the response HTML. The problem is that if the request fails, so does your widget. Only use it on reliable/fast servers.


You have it backwards Gnome Smile He's using XMLHttpRequest in synchronous mode there - the third argument is set to true for async, and false means synchronous (like he's done).

As to the original problem, it sounds like it's possibly an Info.plist problem - have you tried stopping everything called onload and seeing if it loads OK (background shows etc)?
View user's profile Send private message Send e-mail Visit poster's website AIM Address Widgets
gnome
Administrator


Joined: 26 Sep 2004
Posts: 145

Posted: Sun Jul 24, 2005 - 9:55 am    Post subject: Reply with quote

Oh, haha, oops. Apologies.
View user's profile Send private message Widgets
TomGillespie



Joined: 23 Jul 2005
Posts: 3

Posted: Sun Jul 24, 2005 - 4:07 pm    Post subject: Reply with quote

I have it working now, by changing the path to the file it opens. It was originally pointing to a www.domain1.com/file.php, where domain1.com was a redirect to www.domain2.com/domain1/file.php.


I'm guessing it wasnt working because of the way the redirect script worked or something.


Thanks guys!
View user's profile Send private message Send e-mail Widgets
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.

 
Powered by phpBB © 2001, 2002 phpBB Group