0

Here is the code copied from w3schools (http://www.w3schools.com/ajax/tryit.asp?filename=tryajax_suggest):

<html>
<head>
<script type="text/javascript">
function showHint(str)
{
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
	  if (xmlhttp.readyState == 4)
	  {
		  alert("I am ready status: "+xmlhttp.status);		  	
	  }

	    if (xmlhttp.readyState==4 && xmlhttp.status==200)
    	{
    		document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
	    }
  }
xmlhttp.open("GET","http://www.w3schools.com/ajax/gethint.asp?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>

<h3>Start typing a name in the input field below:</h3>
<form action=""> 
First name: <input type="text" id="txt1" onkeyup="showHint(this.value)" />
</form>
<p>Suggestions: <span id="txtHint"></span></p> 

</body>
</html>

I saved this as firstAjax.html in my hard disk.

I typed the url:
http://www.w3schools.com/ajax/gethint.asp?q=u

and get result "Unni". Then I modified firstAjax.html and inserted the url (as can be seen from above). When I type something in the text box of firstAjax.html, nothing shows up. So I put an alert to show the status. It prints:

"I am ready status:0".

This is weird, isn't it? Because the same url can give results from firefox. But the same url can not work for the same code from the same source. I checked javascript error console which is clean. Besides as it is entering here:

xmlhttp.onreadystatechange=function()
  {...}

so the browser supports ajax.

Some correction, I tried the code with IE8 where it works. The firefox is the latest one 3.6.3 where it doesn't work. Is there any security settings that need to be enabled to make it work? As it is the recent one (2010 release) and it doesn't complain about manipulation of xmlhttp so it is supposed to work (gmail and others work). Got any ideas?

Edited by johndoe444: n/a

8
Contributors
9
Replies
19
Views
7 Years
Discussion Span
Last Post by bharathivkmani
0

It isn't your code. A very detailed test program fails here in a similar way.
I can get the w3schools hint in Safari (PC) and IE8,
I can't get it in Chrome, Firefox or Opera.
I strongly suspect that the problem is cross-domain blocking (which conforming user agents are supposed to do). It that is the case, however, it is more than a little annoying that there is no clear error message (and, needless to say, I can't explain why two browsers handle the entire exchange without difficulty).

BTW: the w3schools 'hint' page itself doesn't work here in those three browsers either.

Edited by fxm: n/a

0

Sure enough, when I point the request to a URI in my own domain, processing no longer stops at status=0.

0

Yeah I figured it out today. It's because of this thing:

"Any discussion of browser-based technologies wouldn’t be complete without mentioning security.
The XMLHttpRequest object is subjected to the browser’s security “sandbox.” Any resources
requested by the XMLHttpRequest object must reside within the same domain from which the
calling script originated. This security restriction prevents the XMLHttpRequest object from
requesting resources outside the domain from which the script was originally served.
The strength of this security restriction varies by browser (see Figure 2-5). Internet
Explorer shows an alert stating that a potential security risk exists but gives the user a choice
of whether to continue with the request. Firefox simply stops the request and shows an error
message in the JavaScript console."

Ref: Foundations of Ajax

0

This thread is too important to be allowed to die. It solves one of the most annoying problems with AJAX. And, it is difficult to find documentation on this problem. Kudos to fxm and johndoe444.

0

To designer-fixit . . . assuming you are getting this same problem in a local development environment (i.e. I am running the Xampp package on my local computer), the problem can be found in the url of the web browser you are trying to run the program in. If it is defaulting to file:///<pathname> (Firefox) or file:\\\<pathname> (IE), then the xmlHttpRequest.status == 0. If you change it to run as http://localhost/<pathname>, then the problem disappears and the xmlHttpRequest.status is set as expected. Hope this helps.

0

Posted this snippet in a similar post - not tidy but helped me get the information into the html file.

This is the forum post I made earlier
http://www.daniweb.com/forums/thread315732.html

and below is the code snippet that I found worked for me

if (window.XMLHttpRequest) {
		//for firefox, opera and safari browswers
		var xmlHttp = new XMLHttpRequest();
	}
	if (typeof XMLHttpRequest == "undefined")
  XMLHttpRequest = function () {
    try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); }
      catch (e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); }
      catch (e) {}
    try { return new ActiveXObject("Msxml2.XMLHTTP"); }
      catch (e) {}
    //Microsoft.XMLHTTP points to Msxml2.XMLHTTP.3.0 and is redundant
    throw new Error("This browser does not support XMLHttpRequest.");
  };
	
	
/*@cc_on @if(@_jscript)
var xmlHttp=new ActiveXObject("Microsoft.XMLDOM");
xmlHttp.async="false";
xmlHttp.load("sellers.xml");
@else */
xmlHttp.open("GET","sellers.xml",false);
xmlHttp.send(false);
var xmlDoc= xmlHttp.responseXML;
/*@end
@*/

If there is a prettier code - would love to know it - I keep this one saved so I can repurpose it and never have to fully type it again. :)

Edited by macgurl70: was late - typos like crazy

0

Just place your page under a Web Server in order to avoid the file// protocol.. looks like if the browser detects something like file:///myAjax.html it fails, on the other hand if it is http://myserver/myAjax.html it sends code 200 rather than 0 in the status..

0

still i am getting status = 0 error . i have changed the url as u tell but still the page is not loading ......... please assist me

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.