Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 1 of 1

Thread: ezjax

  1. #1
    Regular Coder Jazzo's Avatar
    Join Date
    Apr 2008
    Location
    New York City
    Posts
    164
    Thanks
    20
    Thanked 2 Times in 2 Posts

    ezjax

    This is just a very-mini-library/convenience function that sends and receives AJAX information using get/post. It also supports using an object instead of a query string. It's long because it checks for all errors. The two functions are as follows:

    ezjax.get(url, query, func);
    ezjax.post(url, query, sendVars, func);

    "url" is the URL without any query string. "query" is either a query string or an object which gets converted to a query string. "sendVars" is the same; it is the argument of request.send() and it can be an object. "func" is the function that is called after the request is complete. It takes one argument.

    Code:
    // Code by Julian Rosenblum
    
    var ezjax = (function () {
      var open,
      createReq = function () {
        if (window.XMLHttpRequest) {
          return new XMLHttpRequest();
        }
        else if (window.ActiveXObject) { 
          try {
            return new ActiveXObject('Msxml2.XMLHTTP');
          } catch (e) {
            return new ActiveXObject('Microsoft.XMLHTTP');
          }
        }
        else {
          throw new Error('Browser does not support Ajax');  
        }
      };
      open = function (req, type, url, urlVars, sendVars, func) {
        var urlString,
        sendString,
        toQueryString = function (obj) {
          var i,
          retVal = '?';
          if (!obj) {
            return '';  
          }
          else if (typeof obj === 'string') {
            if (obj.indexOf('?') === 0) {
              return obj;    
            }
            else {
              return '?' + obj;
            }
          }
          else if (typeof obj === 'object') {
            for (i in obj) {
              if (obj.hasOwnProperty(i)) {
                retVal += escape(i) + '=' + escape(obj[i]) + '&';
              }
            }
            return retVal.slice(0, -1);
          }
          else {
            throw new TypeError();  
          }
        };
        urlString = url + toQueryString(urlVars);
        sendString = (toQueryString(sendVars) === '?' ? null : toQueryString(sendVars).slice(1));
        req.open(type, urlString, true);
        if (type === 'POST') {
          req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');    
        }
        req.onreadystatechange = function () {
          if (req.readyState === 4) {
            if (req.status === 200) {
              if (func) {
                func(req.responseText);
              }
            }
          }
        };
        req.send(sendString);
      };
      return {
        get: function (url, urlVars, func) {
          var req = createReq();
          open(req, 'GET', url, urlVars, null, func);
        },
        post: function (url, urlVars, sendVars func) {
          var req = createReq();
          open(req, 'POST', url, urlVars, sendVars, func);
        }
      };
    }());
    Last edited by Jazzo; 12-24-2010 at 04:45 PM.
    ~Julian
    14 y/o web developer, drummer, and Bridge player


 

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •