<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: concurrent code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 18 May 2008 10:42:49 GMT</pubDate>
    <description>DZone Snippets: concurrent code</description>
    <item>
      <title>Javascript Ajax Object</title>
      <link>http://snippets.dzone.com/posts/show/3650</link>
      <description>&lt;code&gt;&lt;br /&gt;// Just a stub function we'll tell ajaxObject to call when it's done&lt;br /&gt;// callback functions get responseText, and responseStat respectively&lt;br /&gt;// in their arguments.&lt;br /&gt;function fin(responseTxt,responseStat) {&lt;br /&gt;  alert(responseStat+' - '+responseTxt);&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;// create a new ajaxObject, give it a url it will be calling and&lt;br /&gt;// tell it to call the function "fin" when its got data back from the server.&lt;br /&gt;var test1 = new ajaxObject('http://someurl.com/server.cgi',fin);&lt;br /&gt;    test1.update();&lt;br /&gt;		&lt;br /&gt;// create a new ajaxObject, give it a url and tell it to call fin when it&lt;br /&gt;// gets data back from the server.  When we initiate the ajax call we'll&lt;br /&gt;// be passing 'id=user4379' to the server.		&lt;br /&gt;var test2 = new ajaxObject('http://someurl.com/program.php',fin);&lt;br /&gt;    test2.update('id=user4379');&lt;br /&gt;		&lt;br /&gt;// create a new ajaxObject but we'll overwrite the callback function inside&lt;br /&gt;// the object to more tightly bind the object with the response hanlder.&lt;br /&gt;var test3 = new ajaxObject('http://someurl.com/prog.py', fin);&lt;br /&gt;    test3.callback = function (responseTxt, responseStat) {&lt;br /&gt;      // we'll do something to process the data here.&lt;br /&gt;      document.getElementById('someDiv').innerHTML=responseTxt;&lt;br /&gt;    }&lt;br /&gt;    test3.update('coolData=47&amp;userId=user49&amp;log=true');	&lt;br /&gt;		&lt;br /&gt;// create a new ajaxObject and pass the data to the server (in update) as&lt;br /&gt;// a POST method instead of a GET method.&lt;br /&gt;var test4 = new ajaxObject('http://someurl.com/postit.cgi', fin);&lt;br /&gt;    test4.update('coolData=47&amp;userId=user49&amp;log=true','POST');	&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;function ajaxObject(url, callbackFunction) {&lt;br /&gt;  var that=this;      &lt;br /&gt;  this.updating = false;&lt;br /&gt;  this.abort = function() {&lt;br /&gt;    if (that.updating) {&lt;br /&gt;      that.updating=false;&lt;br /&gt;      that.AJAX.abort();&lt;br /&gt;      that.AJAX=null;&lt;br /&gt;    }&lt;br /&gt;  }&lt;br /&gt;  this.update = function(passData,postMethod) { &lt;br /&gt;    if (that.updating) { return false; }&lt;br /&gt;    that.AJAX = null;                          &lt;br /&gt;    if (window.XMLHttpRequest) {              &lt;br /&gt;      that.AJAX=new XMLHttpRequest();              &lt;br /&gt;    } else {                                  &lt;br /&gt;      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");&lt;br /&gt;    }                                             &lt;br /&gt;    if (that.AJAX==null) {                             &lt;br /&gt;      return false;                               &lt;br /&gt;    } else {&lt;br /&gt;      that.AJAX.onreadystatechange = function() {  &lt;br /&gt;        if (that.AJAX.readyState==4) {             &lt;br /&gt;          that.updating=false;                &lt;br /&gt;          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        &lt;br /&gt;          that.AJAX=null;                                         &lt;br /&gt;        }                                                      &lt;br /&gt;      }                                                        &lt;br /&gt;      that.updating = new Date();                              &lt;br /&gt;      if (/post/i.test(postMethod)) {&lt;br /&gt;        var uri=urlCall+'?'+that.updating.getTime();&lt;br /&gt;        that.AJAX.open("POST", uri, true);&lt;br /&gt;        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");&lt;br /&gt;        that.AJAX.send(passData);&lt;br /&gt;      } else {&lt;br /&gt;        var uri=urlCall+'?'+passData+'&amp;timestamp='+(that.updating.getTime()); &lt;br /&gt;        that.AJAX.open("GET", uri, true);                             &lt;br /&gt;        that.AJAX.send(null);                                         &lt;br /&gt;      }              &lt;br /&gt;      return true;                                             &lt;br /&gt;    }                                                                           &lt;br /&gt;  }&lt;br /&gt;  var urlCall = url;        &lt;br /&gt;  this.callback = callbackFunction || function () { };&lt;br /&gt;}&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Fri, 09 Mar 2007 03:06:18 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3650</guid>
      <author>pcx99 (Patrick)</author>
    </item>
  </channel>
</rss>
