garethhall Posted December 1, 2010 Share Posted December 1, 2010 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 More sharing options...
trq Posted December 1, 2010 Share Posted December 1, 2010 If you pass in a third argument to your success function it will be the XMLHttpRequest object. Link to comment https://forums.phpfreaks.com/topic/220402-jquery-ajax-help/#findComment-1142024 Share on other sites More sharing options...
garethhall Posted December 1, 2010 Author Share Posted December 1, 2010 I have updated my code but obj is undefined $.post( 'ajax/overview.php', {'action' : 'noNewFiles'}, function(data, textStatus, obj){ console.log(obj); $('#newFiles').text(data); } ); Link to comment https://forums.phpfreaks.com/topic/220402-jquery-ajax-help/#findComment-1142029 Share on other sites More sharing options...
haku Posted December 2, 2010 Share Posted December 2, 2010 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 More sharing options...
garethhall Posted December 2, 2010 Author Share Posted December 2, 2010 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 More sharing options...
trq Posted December 2, 2010 Share Posted December 2, 2010 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: $.post provides exactly the same callback functionality. Link to comment https://forums.phpfreaks.com/topic/220402-jquery-ajax-help/#findComment-1142068 Share on other sites More sharing options...
haku Posted December 2, 2010 Share Posted December 2, 2010 The point was, with the callback function you don't need to know the readystate. jQuery takes care of that in the background for you. Link to comment https://forums.phpfreaks.com/topic/220402-jquery-ajax-help/#findComment-1142219 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.