Jump to content

AJAX Error

- - - - -

  • Please log in to reply
6 replies to this topic

#1
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I am trying to get AJAX script to retrieve a webpage and put its contents into a DIV.
I made the following code:
<html> 

	<head> 

		<title> AJAX 1 </title> 

		<script type="text/javascript"> 

			var http; 

			if (window.XMLHttpRequest){ 

				http= new XMLHttpRequest(); 

			} else { 

				http= new ActiveXObjec("Microsoft.XMLHTTP"); 

			} 

		</script> 

	</head> 

	<body> 

		<h1> AJAX 1 </h1> 

		<div id="txt">   </div> 

		<script type="text/javascript"> 

			http.open("GET", "http://www.google.com/webhp?hl=en&q=google", 0); 

			http.send(); 

			document.getElementById("txt").innerHTML= http.responseText; 

		</script> 

	</body> 

</html> 
but it says something about an uncaught exception, when I test it in Firefox. What's wrong?

#2
BlaineSch

BlaineSch

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,448 posts
You can't make AJAX requests to different domains then the one running the script.

#3
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Okay, it works now.

The HTML file is located at "http://192.168.0.3/index.html"

The HTML:
<html> 

	<head> 

		<title> AJAX 1 </title> 

		<script type="text/javascript"> 

			var http; 

			if (window.XMLHttpRequest){ 

				http= new XMLHttpRequest(); 

			} else { 

				http= new ActiveXObjec("Microsoft.XMLHTTP"); 

			} 

		</script> 

	</head> 

	<body> 

		<h1> AJAX 1 </h1> 

		<div id="txt">   </div> 

		<script type="text/javascript"> 

			http.open("GET", "http://192.168.0.3/file1.txt", 0); 

			http.send(); 

			document.getElementById("txt").innerHTML= http.responseText; 

		</script> 

	</body> 

</html>


#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Don't you have to wait for status 200 and readystate 4? like

http.onreadystatechange=function()

  {

  if (xmlhttp.readyState==4 && xmlhttp.status==200)

    {

      document.getElementById("txt").innerHTML= http.responseText;

    }

  }



#5
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Not if you call the http.open function with 0 as the third argument.

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Okay, if you do this locally eg, not deployed, It's just local and your computer will never return status 200 as it's no webserver. It'll always be status 0.
Status 200 seems to be however required for chrome (as 200 indicates it's successful)

So:
Chrome does annoying with this and will throw some error 101.
IE is annoying due to cross domain i think ( i get access denied)
Firefox is the only one who works.

I can only suggest to deploy this to a webserver or a local webcontainer may also do, like tomcat / glassfish / whatever and see if the problem persists.

Instead of 'http://192.168.0.3/file1.txt' you can "just" do 'file1.txt' too by the way.

#7
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
That's sort of how my index.html file is, by now:
<html> 

	<head> 

		<title> AJAX 1 </title> 

		<script type="text/javascript"> 

			var http; 

			if (window.XMLHttpRequest){ 

				http= new XMLHttpRequest(); 

			} else { 

				http= new ActiveXObjec("Microsoft.XMLHTTP"); 

			} 

		</script> 

	</head> 

	<body> 

		<h1> AJAX 1 </h1> 

		<div id="txt">   </div> 

		<script type="text/javascript"> 

			function a(){ 

				http.open("GET", "/file1.txt", 0); 

				http.send(); 

				document.getElementById("txt").innerHTML= http.responseText; 

			} 

			var f= a; 

			a(); 

		</script> 

	</body> 

</html>

It's not much different from when I first made it because I mostly worked on other things and changed only a little bit in this file.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users