Jump to content

Jquery ajax help


garethhall

Recommended Posts

Hey guys,

 

I am just in the process on converting a system to use jquery instead of normal javascript. But i need to detect the readystate in the jquery. How do I do that?

 

Normal JS

function theNewFiles(){
  xhrNewFiles=GetXmlHttpObject()
  if(xhrNewFiles==null){
  	alert("Browser does not support HTTP Request");
  	return;
  }
  var qstr;
  qstr = "action=noNewFiles";
  qstr += "&sid="+Math.random();
  xhrNewFiles.onreadystatechange = theNewFilesR;
  xhrNewFiles.open("POST","ajax/overview.php",true);
  xhrNewFiles.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  xhrNewFiles.send(qstr);	
}
function theNewFilesR(){
  if(xhrNewFiles.readyState == 3){
  	document.getElementById('newFiles').innerHTML = '<img src="images/loaderWhiteBG.gif" width="105" height="12" />';
  }else if(xhrNewFiles.readyState==4 || xhrNewFiles.readyState=="complete"){ 
  	document.getElementById('newFiles').innerHTML = xhrNewFiles.responseText;
  }
}

 

Converted to jquery

$.post(
  'ajax/overview.php',
  {'action' : 'noNewFiles'},
  function(data, textStatus){
  	$('#newFiles').text(data);
  }
);  

Link to comment
Share on other sites

If I can make a suggestion, why don't you use $.ajax() instead of $.post(). It's easier to use, and you can specify a callback function:

 

$.ajax(
  url:'ajax/overview.php',
  data:{'action' : 'noNewFiles'},
  success:function(data, textStatus, obj){
         console.log(obj);
     $('#newFiles').text(data);
  }
);

Link to comment
Share on other sites

Hi,

 

I don't think using $.post is any harder to write than $.ajax. That said yes having a callback is useful. I fixed some typos in your code but obj is still undefined

 

 

$.ajax({
  url:'ajax/overview.php',
  type: 'POST',
  data:{'action' : 'noNewFiles'},
  success:function(data, textStatus, obj){
         console.log(obj);
     $('#newFiles').text(data);
  }
});

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.