Jump to content

Problem with AJAX example


GrahamJAT

Recommended Posts

Being new to JavaScript and AJAX, I am working my way through the W3Schools' tutorials on the subject.When trying out the AJAX tutorial (http://www.w3schools.com/Ajax/ajax_serverscript.asp) to retrieve the time of day from the remote web-server using the W3Schools' ASP file 'time.asp', I copied the example files, as instructed, straight from the site and created my own local html file, but changing the reference for 'time.asp' to 'http://www.w3schools.com/Ajax/time.asp'. The code is as shown below:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml"><head><title>AJAX The Server-Side Script</title><script type="text/javascript">function ajaxFunction()  {  var xmlHttp;  try    {    // Firefox, Opera 8.0+, Safari    xmlHttp=new XMLHttpRequest();    }  catch (e)    {    // Internet Explorer    try      {      xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");      }    catch (e)      {      try        {        xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");        }      catch (e)        {        alert("Your browser does not support AJAX!");        return false;        }      }    }    xmlHttp.onreadystatechange=function()      {      if(xmlHttp.readyState==4)        {        document.myForm.time.value=xmlHttp.responseText;        }      }    xmlHttp.open("GET","http://www.w3schools.com/Ajax/time.asp",true);    xmlHttp.send(null);  }</script></head><body><form name="myForm">Name: <input type="text"onkeyup="ajaxFunction();" name="username" />Time: <input type="text" name="time" /></form></body></html>

When I load the web-page in Internet Explorer 6, it works as expected.However, It does nothing in Opera 9, and FireFox 2 reports an 'Error' whenever any text entry is made. FireBug shows the error is generated between the last 2 Javascript lines (xmlHttp.open... and 'xmlHttp.send...') but I cannot determine how or what the error is due to.When I run the W3Schools' web page example from their own site (which appears to be the self same code - with a lot of other stuff that makes up their page) it runs properly on all 3 browsers.I do not understand what is going on.Can anyone offer any help?

Link to comment
Share on other sites

You're not allowed to access remote files using AJAX, just files within your same domain. It's for security.
Thanks for that info, Ingolme. I guess that would explain it. The fact that Internet Explorer allows such access doesn't surprise me.I think W3Schools ought to make mention of it.However, could you please be more specific on a couple of things:What files have to be in the same domain? Is it that the .ASP file must be in the same domain as the file containing the javascript, or what?Also, you say it is for security; but what is the issue regarding security that makes it necessary to have the files in the same domain?Cheers.
Link to comment
Share on other sites

What files have to be in the same domain? Is it that the .ASP file must be in the same domain as the file containing the javascript, or what?
Yes
Link to comment
Share on other sites

:) I see no it must be in the same domain as the page that executes the JS.
Link to comment
Share on other sites

:) I see no it must be in the same domain as the page that executes the JS.
That's right, the resource that is accessed using AJAX (the PHP, ASPX, etc.) must be on the same domain as the page that is executing the javascript.If you want to access resources outside of the current domain, you'd have to set up an AJAX proxy. This is where the javascript requests something from a page in the same domain, but that page then forwards the request outside of the domain, processes the response, and then sends it back to the javascript.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...