11 Replies - 1973 Views - Last Post: 05 July 2009 - 01:08 PM

#1 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,108
  • Joined: 07-September 06

Ajax calls not being made after first one

Posted 29 June 2009 - 01:17 AM

Okay, I am in the process of making an Ajax library for jBeta, and have run into a snag.

Here is the relevant code:
jBeta.Ajax = function(callback){

  this.callbackFunction = callback;
  this.queries = [];
  this.queriesInProgress = 0;
  var browser = navigator.appName;
  if(browser == "Microsoft Internet Explorer"){
    this.http = new ActiveXObject("Microsoft.XMLHTTP");
  }
  else{
    this.http = new XMLHttpRequest();
  }

  this.ushift = function(){
    var retArr = [];
    for(var i=1; i<this.queries.length; i++){
      retArr[i] = this.queries[i];
    }
    this.queries = retArr;
  };

  this.checkQueries = function(){
    if(this.queriesInProgress < 1 && this.queries.length > 0){
      var next = this.queries[0];
      this.ushift();
      this.call(next);
    }
  };

  this.call = function(queryObj){
    if(this.queriesInProgress >= 1){
      this.queries.push(queryObj);
    }
    else{
      alert("Making query to "+queryObj['url']);
      this.queriesInProgress++;
      if(queryObj['method'].toUpperCase() == 'GET'){
        this.http.open(queryObj['method'], queryObj['url']+ ((queryObj['params'] != '')?('?'+queryObj['params']):''), true);
        this.http.send(null);
      }
      else{
        this.http.open(queryObj['method'], queryObj['url'], true);
        this.http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        this.http.setRequestHeader("Content-length", queryObj['params'].length);
        this.http.setRequestHeader("Connection", "close");
        this.http.send(queryObj['params']);
      }
    }
  };

  this.handleResponse = function(){
    if(this.http.readyState == 4 && this.http.status == 200) {
      this.callbackFunction(this.http.responseText);
      this.queriesInProgress--;
      this.checkQueries();
    }
  };
  var t = this;
  this.http.onreadystatechange = function(){t.handleResponse()};

}


<script>
	  var ajax = new jBeta.Ajax(function(info){
		alert(info);
	  });
	  ajax.call({'method': 'get', 'url': 'http://www.dreamincode.net/forums/index.php', 'params': 'automodule=blog&req=syndicate'});
	  ajax.call({'method': 'get', 'url': 'http://www.dreamincode.net/forums/index.php', 'params': 'automodule=blog&req=syndicate'});
</script>



NOTE - I have stripped everything not-necessary for this code out. If you want to have the rest for testing it can b downloaded from the jBeta project thread in the projects forum (everything is the same except for this Ajax stuff, which I am adding).

Now, as you can tell the two calls are the same. The first one returns fairly quickly, but the second one never returns, and I haven't got a clue as to why (it is late, so I may have missed something). What should be happening here, is that you can have 1 Ajax call going at a time (I know the limit is 2, this is just for dev purposes), when you get multiple calls all the subsequent ones should be added to a queue and then the library should get to them in order (seems to work, it calls the call() function 3 times). For some reason the later calls (after the first one) are never made (I am assuming that is the problem, they should return if they are successfully sent, one would think).

Hopefully a fresh pair of eyes can help me find the issue :)

Thanks in advance.

Is This A Good Question/Topic? 0
  • +

Replies To: Ajax calls not being made after first one

#2 arthurakay  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 17-February 09

Re: Ajax calls not being made after first one

Posted 29 June 2009 - 01:23 PM

I don't see anything immediately apparent... but I do have a suggestion: using Firebug's console, make sure your code is actually launching the second request.

I say this because browsers are finicky with JS code. If there's an error encountered anywhere in a function, the browser will simply refuse to execute it a second time. I don't know the exact mechanism, but I think the browser (all browsers I know of) remembers that an error happened in that method and won't bother to try again.

The point is that IF there's an error in your method, the browser won't run the code a second time... hence why your subsequent calls are never made. If the second request is never launched, you should be able to tell that your code is failing:

1) Because the server response itself is not expected/handled correctly, or
2) There's a syntax error in the response handler
Was This Post Helpful? 0
  • +
  • -

#3 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,108
  • Joined: 07-September 06

Re: Ajax calls not being made after first one

Posted 29 June 2009 - 03:13 PM

I did hae a problem with offsite ajax requests, but have since fixed that. Now, however I still have a problem. While it seems to work in FF at this point IE still has the same issue (and there are no errors in FF's Firebug nor error console, nor is IE complaining about anything).

I have uploaded a full example of what it is doing (there are a few alerts, for debug purposes):
http://www.reigningg...le_v1.0.1b.html

This problem is showing up in IE 6, I don't know about the others.

How to tell if it is working:
There is a link toward the bottom of the content that says "Show Modal" if it is working correctly it should say:
"Show ModalFroggy flies fast :)Froggy flies fast :)", or something along those lines.
In IE 6 is stops with the first response...

PS - works in Chrome too.
Was This Post Helpful? 0
  • +
  • -

#4 arthurakay  Icon User is offline

  • D.I.C Head

Reputation: 22
  • View blog
  • Posts: 226
  • Joined: 17-February 09

Re: Ajax calls not being made after first one

Posted 30 June 2009 - 06:28 AM

I don't know what to tell you. Based on your description, it seems to work fine for me. That link appears at the bottom in both IE7 and IE8.
Was This Post Helpful? 0
  • +
  • -

#5 thehat  Icon User is offline

  • awake ? web();
  • member icon

Reputation: 106
  • View blog
  • Posts: 951
  • Joined: 28-February 08

Re: Ajax calls not being made after first one

Posted 30 June 2009 - 06:35 AM

It's working in IE6 for me, did you just change something?
Was This Post Helpful? 0
  • +
  • -

#6 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,108
  • Joined: 07-September 06

Re: Ajax calls not being made after first one

Posted 30 June 2009 - 12:48 PM

No, I haven't changed anything, can I get a screenshot of what you guys are seeing in IE?
Was This Post Helpful? 0
  • +
  • -

#7 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,108
  • Joined: 07-September 06

Re: Ajax calls not being made after first one

Posted 04 July 2009 - 06:40 PM

-bump-
Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Ajax calls not being made after first one

Posted 04 July 2009 - 07:00 PM

You could try using ajaxStop() to see if that resolves your issue. Something like

<script>
	  var ajax = new jBeta.Ajax(function(info){
		alert(info);
	  });
	  $.ajaxStop(function(){
			  ajax.call({'method': 'get', 'url': 'http://www.dreamincode.net/forums/index.php', 'params': 'automodule=blog&req=syndicate'});
			  ajax.call({'method': 'get', 'url': 'http://www.dreamincode.net/forums/index.php', 'params': 'automodule=blog&req=syndicate'});
	  });	  
</script>



Now I know $.ajaxStop is jQuery but you could look through their js file to see how it works, then incorporate your own version. Just an idea :)
Was This Post Helpful? 1
  • +
  • -

#9 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,108
  • Joined: 07-September 06

Re: Ajax calls not being made after first one

Posted 04 July 2009 - 10:10 PM

Alrighty, I'll look at it. Their Ajax section is really dense though...
Was This Post Helpful? 0
  • +
  • -

#10 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,108
  • Joined: 07-September 06

Re: Ajax calls not being made after first one

Posted 04 July 2009 - 10:52 PM

Yay! I didn't wind up using the $.ajaxStop function like you suggested, but I found that IE apparently has a memorty leak when dealing with Ajax requests, I have since got the script to run correctly in each of the browsers (FF, IE, Chrome, and Opera).

The solution was to delete the Ajax object after each call and then create a new one when another call is made (in IE only).
Was This Post Helpful? 0
  • +
  • -

#11 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1638
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Ajax calls not being made after first one

Posted 04 July 2009 - 10:57 PM

Glad you were able to find a solution :)
Was This Post Helpful? 0
  • +
  • -

#12 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1138
  • View blog
  • Posts: 7,108
  • Joined: 07-September 06

Re: Ajax calls not being made after first one

Posted 05 July 2009 - 01:08 PM

Thanks, I just found it has an error in IE 7, grr. Guess I need to look and see if there is something else wrong with it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1