4 Replies - 463 Views - Last Post: 02 September 2013 - 10:32 PM

#1 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 148
  • View blog
  • Posts: 830
  • Joined: 26-May 10

Nonresponsive AJAX script

Posted 02 September 2013 - 03:21 PM

I have little experience with AJAX, and having trouble getting it to execute my php script in the background. I've set up a series of alerts to show the scripts progress, but none of them are going off. Can someone tell me what I'm missing?

	var xmlhttp;

	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==1 && xmlhttp.status==200)
    	{
    		alert("1!");
    	}
  		if (xmlhttp.readyState==2 && xmlhttp.status==200)
    	{
    		alert("2!");
    	}
  		if (xmlhttp.readyState==3 && xmlhttp.status==200)
    	{
    		alert("3!");
    	}
  		if (xmlhttp.readyState==4 && xmlhttp.status==200)
    	{
    		alert("ding!");
    	}

		if (xmlhttp.status==404) {alert("file not found");}
  	}
	
	var url = "scripts/updatelastdl.php?lastdl="+latest_version+"&id="+id+"&t="+Math.random();
	xmlhttp.open("GET",url,true);
   xmlhttp.send();


Is This A Good Question/Topic? 0
  • +

Replies To: Nonresponsive AJAX script

#2 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1399
  • View blog
  • Posts: 3,101
  • Joined: 05-April 11

Re: Nonresponsive AJAX script

Posted 02 September 2013 - 04:11 PM

Debug your code. Place a breakpoint at line 14 and check if the variables are what you expect them to be
Was This Post Helpful? 0
  • +
  • -

#3 Ntwiles  Icon User is offline

  • D.I.C Addict

Reputation: 148
  • View blog
  • Posts: 830
  • Joined: 26-May 10

Re: Nonresponsive AJAX script

Posted 02 September 2013 - 04:29 PM

I changed the asynchronous parameter in the open method to 'false' and the script started working as expected, so I believe all the values are what I meant for them to be. This kind of defeats the purpose of AJAX if my page is going to hang until the server responds though, doesn't it?
Was This Post Helpful? 0
  • +
  • -

#4 laytonsdad  Icon User is offline

  • Cheese and Sprinkles
  • member icon

Reputation: 440
  • View blog
  • Posts: 1,867
  • Joined: 30-April 10

Re: Nonresponsive AJAX script

Posted 02 September 2013 - 06:14 PM

I find that doing ajax calls via jQuery much faster, more reliable and cross browser safe. Look in to the .post and .get methods

This post has been edited by laytonsdad: 02 September 2013 - 06:15 PM

Was This Post Helpful? 1
  • +
  • -

#5 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 3541
  • View blog
  • Posts: 10,238
  • Joined: 08-June 10

Re: Nonresponsive AJAX script

Posted 02 September 2013 - 10:32 PM

from the tech specs:

http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060405/#dfn-status said:

status of type unsigned short

If the status attribute is not available it MUST raise an exception. It MUST be available when readyState is 3 (Receiving) or 4 (Loaded). When available, it MUST represent the HTTP status code (typically 200 for a successful connection).

(underlining by me)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1