Jump to content

ajax request script probs


jug

Recommended Posts

Hi, I have been developing this script for a few days with its objective to make ajax requests scripts a lot easier to write each time. The errors Im getting are mainly server readystate and status errors (not being the correct code). Also I am fairly new to making custom JavaScript objects.

 

If anyone could quickly look through the script and point anything out itll be much appreciated.

 

Thanks in advance

 

jug

 

calling the function

 


function process(){

    var XHR = new ajaxRequest(scriptPath, 'XML', response);
    XHR.doPost(params);
  }

  return false;
}

function response(xmlResponse){

  //stuff here
}

 

custom ajax object

 


function ajaxRequest(url, responseType, callback){

  var req = init();
  req.onreadystatechange = processRequest;

  var errorText = 'no errors';
        
  function init(){

    if(window.XMLHttpRequest){
      return new XMLHttpRequest();
    }else{
      errorText = 'Your browser does not support the XMLHttpRequest object.';
    }
  }
    
  function processRequest(){

    if(req.readyState != 4){
      errorText = 'Server ready state is not 4.'; 
    }else{
      if(req.status != 200){
        errorText = 'Server status is not 200.';
      }else{
        switch (responseType){
          case 'Text':
            callback(req.responseText);
            break;
          case 'XML':
            callback(req.responseXML);
            break;
          default:
            errorText = 'Incorrect server response type.';
        } 
      }
    }  
  }


  this.doPost = function(params){

    if(req.readyState == 0 || req.readyState == 4){
      try{
        req.open("POST", url, true);
        req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        req.send(params);
      }catch(e){
        errorText = 'XMLHttpRequest POST url not found.';
      }
    }else{
      setTimeout('doPost(params)', 1000);
    }
  }

  this.doGet = function(){

    if(req.readyState == 0 || req.readyState == 4){
      try{
        req.open("GET", url, true);
        req.send(null);
      }catch(e){
        errorText = 'XMLHttpRequest GET url not found.';
      }
    }else{
      setTimeout('doGet()', 1000);
    }
  }

  this.getErrorText = function(){

    return errorText;
  }
}

 

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.