I solved it! The problem was that for HTTP POST, I needed to provide
the data length and close the data stream. This would explain why the
second request failed every time! Have a look at this resource:

http://www.captain.at/howto-ajax-form-post-request.php

This is how I rewrote the script:
<script>
var http_request = false;
function makeRequest(url, parameters) {
        http_request = false;
        http_request = new XMLHttpRequest();
        var POSTDATA = 'test1=' + encodeURI(parameters) + '&test2=' +
encodeURI(parameters);
        var POST_LENGTH = POSTDATA.length;
        try {
        
netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
    } catch (e) {
                alert("Permission UniversalBrowserRead denied.");
    }
    http_request.onreadystatechange = alertContents;
    http_request.open('POST', url, true);
    http_request.setRequestHeader("Content-type", "application/x-www-
form-urlencoded");
    http_request.setRequestHeader("Connection", "close");
    http_request.setRequestHeader("Content-length", POST_LENGTH);
    http_request.send(POSTDATA);
}
function alertContents() {
      if (http_request.readyState == 4) {
         if (http_request.status == 200) {
            alert(http_request.responseText);
            //result = http_request.responseText;
         } else {
            alert('There was a problem with the request.');
         }
      }
}
</script>

:)

*************
Also, if you checked the resource I mentioned above, you'll notice
that the author claims that file uploads are impossible. Not so with
Mozilla. Check this out:

http://www.xulplanet.com/forum2/viewtopic.php?t=2716&highlight=upload

Cheers everybody. I hope this helps someone out there!


On Aug 5, 9:59 pm, The Quill <[EMAIL PROTECTED]> wrote:
> Thanks Brent. I moved the POSTDATA = ""; line to the location you
> suggested and I had the same issue.
> Here's console output I'm getting:
>
> C:\Users\me\Projects\myxulapp>app3.pyhttp://127.0.0.1:52643/
> 127.0.0.1:50276 - - [05/Aug/2007 21:51:47] "HTTP/1.1 POST /test1" -
> 200 OK
> 127.0.0.1:50276 - - [05/Aug/2007 21:51:50] "HTTP/1.1
> test1=5&test2=Hello%20
> %21POST /test1" - 405 Method Not Allowed
> 127.0.0.1:50276 - - [05/Aug/2007 21:51:51] "HTTP/1.1 POST /test1" -
> 200 OK
> 127.0.0.1:50276 - - [05/Aug/2007 21:51:53] "HTTP/1.1 POST /test1" -
> 200 OK
> 127.0.0.1:50276 - - [05/Aug/2007 21:51:54] "HTTP/1.1 POST /test1" -
> 200 OK
> 127.0.0.1:50276 - - [05/Aug/2007 21:51:55] "HTTP/1.1 POST /test1" -
> 200 OK
> 127.0.0.1:50276 - - [05/Aug/2007 21:51:56] "HTTP/1.1 POST /test1" -
> 200 OK
>
> On Aug 5, 6:37 pm, "Brent Pedersen" <[EMAIL PROTECTED]> wrote:
>
> > On 8/4/07, The Quill <[EMAIL PROTECTED]> wrote:
>
> > > Hi there,
>
> > > I'm trying to get AJAX POST to work properly using a local web.py
> > > server. It seems that I can create a successful POST using the first
> > > AJAX call. The second call fails, and then every subsequent call
> > > works! If I stop the server and restart, I get the same failure on the
> > > second call. I'm thinking this is a problem with my python code, but
> > > if not, could someone out there please correct me?
>
> > > Try it for yourself and you'll see what I mean. Also, you'll need to
> > > test this in Firefox and you might need to change the port to suit
> > > your configuration.
> > > Here's my HTML file:
> > > ******************************************************************
> > > <html>
> > > <head>
> > > <title>Firefox to Python and back...</title></head>
> > > <script>
> > > function alertContents() {
> > >     if (http_request.readyState == 4) {
> > >       if (http_request.status == 200) {
> > >         var string = http_request.responseText;
> > >                 alert(string);
> > >       } else {
> > >         alert('There was a problem with the request.');
> > >       }
> > >     }
> > >         POSTDATA = "";
> > >   }
>
> > > var testvalue1 = '5';
> > > var testvalue2 = 'Hello World!';
> > > var POSTDATA = 'test1=' + escape(testvalue1) + '&test2=' +
> > > escape(testvalue2);
> > > function makeRequest(url, parameters) {
>
> > >    try {
>
> > > netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead");
> > >    } catch (e) {
> > >     alert("Permission UniversalBrowserRead denied.");
> > >    }
>
> > >     http_request = false;
> > >     http_request = new XMLHttpRequest();
> > >     if (!http_request) {
> > >       alert('Cannot create XMLHTTP instance');
> > >       return false;
> > >     }
> > >     http_request.open('POST', url, true);
> > >         http_request.setRequestHeader('content-type', 'text/plain');
> > >         http_request.onreadystatechange = alertContents;
> > >     http_request.send(POSTDATA);
> > >   }
> > > </script>
> > > <body>
> > > <a href="#" onClick='makeRequest("http://127.0.0.1:8080/
> > > view","");'>makeRequest</a>
> > > </body>
> > > </html>
> > > ******************************************************************
>
> > > Here's my PYTHON file: app.py
> > > ******************************************************************
> > > import web, os, sys
>
> > > #load urls and matching classes
> > > urls = (
> > >   '/view', 'view',
> > >   '/test', 'test',
> > >   '/quit','quit'
> > > )
> > > class view:
> > >     def POST(self):
> > >                 print('Hello Test!')
> > > web.internalerror = web.debugerror
> > > if __name__ == "__main__":
> > >         os.spawnv(os.P_NOWAIT,'runxulapp.bat',['',''])
> > >         web.run(urls, globals())
> > > ******************************************************************
>
> > > TIA.
>
> > one reason this could work for someone  else and not for you , and on
> > later requests is the ordering of events. the request readystate goes
> > through 3 states before readystate= 4. and you're setting POSTDATA =
> > "";  even for any readystate. so maybe in somebrowsers readystate 0/1
> > is firing (and clearing POSTDATA) before the data is actually sent.
> > that wont happen if the browser re-uses the request object for later
> > requests (which i think most browsers do).
>
> > in short, try putting POSTDATA = "" inside the if statement that
> > checks for readyState==4.


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"web.py" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/webpy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to