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
https://forums.phpfreaks.com/topic/220402-jquery-ajax-help/
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
https://forums.phpfreaks.com/topic/220402-jquery-ajax-help/#findComment-1142056
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
https://forums.phpfreaks.com/topic/220402-jquery-ajax-help/#findComment-1142063
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.