views:

49

answers:

3

Hello,

how can I do the following by means of jQuery?

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 == 4 && xmlhttp.status == 200) {
        document.getElementByID('statusDisplay').innerHTML = xmlhttp.responseText; // show loaded content
    } else if (xmlhttp.readyState >= 1 && xmlhttp.status == 200) /* if(xmlhttp.readyState>=2 && xmlhttp.status==200) */ {
        document.getElementByID('statusDisplay').innerHTML = '<img src="ajax_load.gif" />'; // show ajax loading image
    }
}
xmlhttp.open("GET", "path/to/file.php", true);
xmlhttp.send();

What I am mainly interested in is how I can retrieve the readyStatea and status and how I can retrieve the response text from within those functions (more or less like this):

$.ajax({url: 'path/to/file.php', async: true, success: function(){
    // how can I get the responseText here?
}, whileLoading: function(){
    // does such a parameter actually exist?
}});

Thanks in advance!

+1  A: 

jQuery does not support a "native" (jQuery'ish) access to readyStates.

There is no interactive callback for instance which could represent a readyState===3.

Anyway, you have access to the responseText in the success callback from .ajax()

$.ajax({
   url:      'some.pl',
   dataType: 'text',
   type:     'GET',
   success:  function(data){
      // data represents xhr.responseText here
   }
});

Anyway, the .ajax() method returns the XMLHttpRequest which you can access if necessary.

var myxhr = $.ajax({});
myxhr._onreadystatechange = myxhr.onreadystatechange;

myxhr.onreadystatechange = function(){
    myxhr._onreadystatechange();
    if(myxhr.readyState === 3) {}  // or whatever
};

This is a possible workaround of that issue. But in general you will have all data and information you need within the ajax event callbacks.
Furthermore is the XMLHttpRequest object passed into a lot of callbacks, like beforeSend, error and success.

See http://api.jquery.com/jQuery.ajax/ for further details.

jAndy
Great, thanks! However, is there maybe a possibility to access the readyState from outside, e. g. by saving $.ajax(...) into a variable..?
arik-so
@arik-so: yes indeed, I updated my answer.
jAndy
Great, thanks a lot!
arik-so
+1  A: 

To answer your first question, success callback function takes few parameters, one of them is returned data, so:

$.ajax({url: 'path/to/file.php', async: true, success: function(data){
    // data
}, whileLoading: function(){
    // there is no whileLoading callback function
}});

To answer your second question, there is no such callback function whileLoading. See the documentation for more info: http://api.jquery.com/jQuery.ajax/

Igor Pavelek
A: 

$.ajax() returns the XmlHttpRequest it generates, so you can access it that way, for example:

var xhr = $.ajax({url: 'path/to/file.php', async: true, success: function(data){
    // use data here for the response
}});
xhr.onreadystatechange = function() {
  if (xhr.readyState == 4 && xhr.status == 200) {
    $('#statusDisplay').html(xhr.responseText);
  } else if (xhr.readyState >= 1 && xhr.status == 200) {
    $('#statusDisplay').html('<img src="ajax_load.gif" />');
  }
};

That what you probably want is beforeSend and the data (fist parameter) in success, like this:

$.ajax({
  url: 'path/to/file.php', 
  beforeSend: function() { 
    $('#statusDisplay').html('<img src="ajax_load.gif" />');
  },
  success: function(data) {
    $('#statusDisplay').html(data);
  }
});
Nick Craver
@Nick - You must've just woken up. I'm going to sleep since I can't keep up with a robot :) haha
Marko
@Marko - Aye, about 3 minutes ago :)
Nick Craver
@Nick - Thank god for different time zones. Until they upgrade you to the `no-sleep robot`.
Marko