View Single Post
  #7 (permalink)  
Old 05-08-08, 09:05 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
One thing I've noticed about most AJAX handlers out there is that they typically do GET requests...often I have to pass a substantial amount of data 10K, 100K, etc) and GET may fail silently or (even worse) truncate the data when that much data is passed (depending on the browser). I've gone to using POST for most of my AJAX stuff now. The code below is a simple example of what I use....I'm posting it in the hopes that perhaps it may help someone here:

HTML Code:
<script language="Javascript">
function xmlhttpPost(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            updatepage(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(getquerystring());
}

function getquerystring() {
    var form = document.forms['foo'];
    var word = form.word.value;
    qstr = 'w=' + escape(word); 
    return qstr;
}

function updatepage(str){
    document.getElementById("result").innerHTML = str;
}
</script>
And a sample form:

HTML Code:
<form name="foo">
  <p>word: <input name="word" type="text">  
  <input value="Go" type="button" onclick='JavaScript:xmlhttpPost("/somepage.php")'></p>
  <div id="result"></div>
</form>
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote