Re: RE: AJAX with POST

From: Date: Sun, 05 Apr 2009 04:20:51 +0000
Subject: Re: RE: AJAX with POST
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-291085@lists.php.net to get a copy of this message
Ajax can be both async and sync. Itsbthe fourth param in the open call and I believe by default it's a sync call not async Bastien Sent from my iPod On Apr 4, 2009, at 21:33, Skip Evans <skip@bigskypenguin.com> wrote:
But my function using GET does seem to wait. Granted I cobbled it together from various samples and didn't author it from my own deep understanding of the exact process, but here's the snippet that does the real work. req.open('GET', url, false); req.send(null); if(req.responseText) { if(req.responseText.substring(0,7) == 'debug!!') {
      alert(req.responseText.substring(7));
} } return(req.responseText); It seems to wait until it has data to return, because it works perfectly. I can send it a URL from another function and get the data back from server to the function as expected. The only part of it I'm unsure of is this: req.send(null); What does that do? As I said, I cobbled this function from examples, got it working, and presto, was off and running. Skip Brad Broerman wrote:
Well, as the "A" in Ajax is asynchronous, there's no real way to make it wait. What you would normally do is use a callback: function createXHRObject( ) {
       if (typeof XMLHttpRequest != "undefined")
       {
           return new XMLHttpRequest();
       }
       else if (typeof ActiveXObject != "undefined")
       {
           return new ActiveXObject("Microsoft.XMLHTTP");
       }
       else
       {
           throw new Error("XMLHttpRequest not supported");
       }
} function sendAjaxRequest( websvcurl , params, callbackFn ) {
       var xhrObject = createXHRObject();
       xhrObject.open("POST", websvcurl, true);
       xhrObject.onreadystatechange = function()
       {
           if (xhrObject.readyState == 4)
           {
               if( xhrObject.responseXML != null )
               {
                       callbackFn (xhrObject.responseXML);
               }
           }
       }
       xhrObject.setRequestHeader("Content-type",
"application/x-www-form-urlencoded");
       xhrObject.setRequestHeader("Content-length", params.length);
       xhrObject.setRequestHeader("Connection", "close");
       xhrObject.send(params);
} -----Original Message----- From: Skip Evans [mailto:skip@bigskypenguin.com] Sent: Saturday, April 04, 2009 5:30 PM To: php-general@lists.php.net Subject: AJAX with POST Hey all, At the risk of being told this is a PHP and not a JS list, but also knowing the discussions on this list, to the benefit of all I believe, very wildly, I'm posting this JS code snippet for some advice. As I posted earlier, my AJAX app that uses a GET to post to the server (and get a response), fails on IE with larger data, so I thought I'd take a shot at writing a POST function, but so far I can get it to get the data back, but the problem is by the time the data has come back the function has already returned null back to the calling function. What I need this function to do is wait for the data to come back and then send it back to caller. Here's the function. Any advice would be greatly appreciated. (The code to get the appropriate object per browser has been omitted.) http.open("POST", url, true); //Send the proper header information along with the request http.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); http.setRequestHeader("Content-length", url.length); http.setRequestHeader("Connection", "close"); http.send(url); http.onreadystatechange = function() {//Call a function when the state changes. if(http.readyState == 4 && http.status == 200) { alert('['+http.responseText+']'); return (http.responseText); } } -- ==================================== Skip Evans Big Sky Penguin, LLC 503 S Baldwin St, #1 Madison WI 53703 608.250.2720 http://bigskypenguin.com ------------------------------------ Those of you who believe in telekinesis, raise my hand. -- Kurt Vonnegut
--==================================== Skip Evans Big Sky Penguin, LLC 503 S Baldwin St, #1 Madison WI 53703 608.250.2720 http://bigskypenguin.com ------------------------------------ Those of you who believe in telekinesis, raise my hand. -- Kurt Vonnegut --PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


« previous php.general (#291085) next »