views:

407

answers:

2

This post below pretty much says it all. The only issue is the solution (I'm looking at Randy's solution in the comments section) is ASP.Net specific and I'm testing a ColdFusion website using WatiN. So I need a solution that will work with ColdFusion…

http://pushpontech.blogspot.com/2008/04/ajax-issues-with-watin.html

I know I could use Thread.Sleep() and pause for a set amount of time but I would really like to have a solution that will wait for the AJAX postback to complete and continue running immediately afterwards.

Ideally, I would like to find a javascript call that I can use to replace of the ASP.Net specific “Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack()” call.

Any thoughts?

Update:

@CF Jedi Master Thanks for your reply. Allow me to clarify. Basically, I know that when I perform a specific action, it invokes an AJAX request. I would like to know if there is any JavaScript I could run (on the client side) to tell me that the AJAX request has completed. Could I use JavaScript to query for the current HTTP-Request and check that it matches XMLHttpRequest? If so, you're solution may work for me.

@Peter Boughton "If you want to hijack/monitor HTTP requests on the client, you will need proxy software that the connection has been explicitly configured to use...You cannot do this with JavaScript in a web browser."

Yes, that's exactly it. I want to monitor the AJAX request from the client to see when it has completed. I guess it makes since that you cannot do this in JavaScript since the ASP.Net solution also requires a call back to the web server to see if the request is has completed.

So I guess the question becomes: Does ColdFusion support a method or proxy (like the ASP.Net get_isInAsyncPostBack() method) that I can use to monitor the status of an AJAX request? or will this require the CF developers to create a custom 'hook' in the system that will to return the AsyncPostBack state?

@Peter Boughton: Thanks again for your input. I'd like to give you the accepted answer but as you point out: If you definitely want a CF-based (i.e. extJS) method, you'll have to wait for an equivalent answer from someone that uses that.

+2  A: 

It sounds like you are saying - is there a way to tell if the current request is being done via Ajax? Is that right? If so, you can't. An Ajax request is nothing more than another HTTP request. You can check for a HTTP named X-Requested-With. If the value is XMLHttpRequest than you are (possibly!) in an Ajax based request. It isn't full proof, but should work in general.

Update: I've got to say, I'm still a bit confused. You seem to be asking 2 very different things:

"I would like to know if there is any JavaScript I could run (on the client side) to tell me that the AJAX request has completed."

Absolutely. Depending on what framework you use, you have the ability to say, run so and so ajax request, and when done, run this function. jQuery makes this fairly simple for example.

"Could I use JavaScript to query for the current HTTP-Request and check that it matches XMLHttpRequest?"

That to me makes no sense. I've read it a few times and just don't get what you mean. Sorry.

CF Jedi Master
I update to original question to be more clear.
MoMo
A: 

There is no "current HTTP-Request" - you can have multiple requests running at the same time.

JavaScript's XmlHttpRequest can send and receive requests, and that's what it does.

If you want to hijack/monitor HTTP requests on the client, you will need proxy software that the connection has been explicitly configured to use. You cannot do this with JavaScript in a web browser.

Update:

Does ColdFusion support a method or proxy (like the ASP.Net get_isInAsyncPostBack() method) that I can use to monitor the status of an AJAX request?

No idea; I don't use the CF's ajax tools, I use jQuery, and haven't needed a specific check on this.

With jQuery, you can use the .ajax* events to setup a variable for this, for example something like this should achieve that:

var isInAsyncPostBack = false;
jQuery.ajaxStart( function(){ isInAsyncPostBack = true; } )
jQuery.ajaxStop( function(){ isInAsyncPostBack = false; } )

If you definitely want a CF-based (i.e. extJS) method, you'll have to wait for an equivalent answer from someone that uses that.

Peter Boughton
Thanks for your time and input on this...
MoMo