POST data with XMLHttpRequest to CGI (Mozilla)

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jarson

    #1

    POST data with XMLHttpRequest to CGI (Mozilla)

    My JavaScript is trying to POST data to a CGI script (Perl) using
    XMLHttpRequest. My CGI server gets different data from IE than Mozilla
    Firefox.

    // For Mozilla, req = new XMLHttpRequest( );
    // For IE req = new ActiveXObject(" Microsoft.XMLHT TP");
    req.onreadystat echange = requestHandler ; // function to handle async
    response
    req.open('POST' , myURL, true); // use POST
    req.send('foo=1 1&bar=22') ;

    A Perl CGI script prints the parameters passed to it.
    $q = new CGI ;
    foreach my $param ($q->param) {
    print "$param: " . $q->param($param ) . "\n" ;
    }

    The data received by the CGI script is inconsistent, depending if the client
    is IE or Mozilla (Firefox)
    Server result from IE client:
    foo: 11
    bar: 22
    Server result from Mozilla Firefox client:
    POSTDATA: foo=11&bar=22

    It seems that the POST data IE sends is more correct than the Mozilla data.
    Is there another way to send the data in Mozilla so the CGI script will give
    the same results. I could easily adjust the CGI script, but I think the
    problem is at the client.

    Jarson (jarson from sygration, that's a dot com company if you need to send
    an email)





  • Andy Hassall

    #2
    Re: POST data with XMLHttpRequest to CGI (Mozilla)

    On Sat, 30 Apr 2005 12:55:08 -0400, "Jarson" <jarsonk@nospam .com> wrote:
    [color=blue]
    >My JavaScript is trying to POST data to a CGI script (Perl) using
    >XMLHttpRequest . My CGI server gets different data from IE than Mozilla
    >Firefox.
    >
    >// For Mozilla, req = new XMLHttpRequest( );
    >// For IE req = new ActiveXObject(" Microsoft.XMLHT TP");
    > req.onreadystat echange = requestHandler ; // function to handle async
    >response
    > req.open('POST' , myURL, true); // use POST
    > req.send('foo=1 1&bar=22') ;
    >
    >A Perl CGI script prints the parameters passed to it.
    > $q = new CGI ;
    > foreach my $param ($q->param) {
    > print "$param: " . $q->param($param ) . "\n" ;
    > }
    >
    >The data received by the CGI script is inconsistent, depending if the client
    >is IE or Mozilla (Firefox)
    >Server result from IE client:
    > foo: 11
    > bar: 22
    >Server result from Mozilla Firefox client:
    > POSTDATA: foo=11&bar=22
    >
    >It seems that the POST data IE sends is more correct than the Mozilla data.
    >Is there another way to send the data in Mozilla so the CGI script will give
    >the same results. I could easily adjust the CGI script, but I think the
    >problem is at the client.[/color]

    Aren't you missing setting the content-type for your request, that is, using:

    req.setRequestH eader('Content-Type', 'application/x-www-form-urlencoded;');

    Perhaps one browser fills in the blanks whereas the other doesn't; either way
    you ought to explicitly state it when POSTing data.

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • Martin Honnen

      #3
      Re: POST data with XMLHttpRequest to CGI (Mozilla)



      Jarson wrote:
      [color=blue]
      > My JavaScript is trying to POST data to a CGI script (Perl) using
      > XMLHttpRequest. My CGI server gets different data from IE than Mozilla
      > Firefox.
      >
      > // For Mozilla, req = new XMLHttpRequest( );
      > // For IE req = new ActiveXObject(" Microsoft.XMLHT TP");
      > req.onreadystat echange = requestHandler ; // function to handle async
      > response
      > req.open('POST' , myURL, true); // use POST[/color]

      First make the open call, then set the onreadystatecha nge handler and
      then before you send the data set the HTTP request header e.g.
      if (typeof req.setRequestH eader != 'undefined') {
      req.setRequestH eader('Content-Type',
      'application/x-www-form-urlencoded');
      }
      [color=blue]
      > req.send('foo=1 1&bar=22') ;[/color]

      as that seems to be the content type of the data you want to post.


      --

      Martin Honnen
      http://JavaScript.FAQTs.com/

      Comment

      • Jarson

        #4
        Re: POST data with XMLHttpRequest to CGI (Mozilla)

        MAGIC!! Adding setRequestHeade r to my JavaScript fixed it!

        setRequestHeade r was missing from my code and all of the documentation and
        samples I've read!
        Unscientific research:

        Google for +XMLHttpRequest +send = 65,500 results
        Google for +XMLHttpRequest +setRequestHead er = 516 results

        Thanks Martin and Andy for that piece of valuable information!
        Jarson

        "Martin Honnen" <mahotrash@yaho o.de> wrote in message
        news:4273c110$0 $7534$9b4e6d93@ newsread2.arcor-online.net...[color=blue]
        >
        >
        > Jarson wrote:
        >[color=green]
        >> My JavaScript is trying to POST data to a CGI script (Perl) using
        >> XMLHttpRequest. My CGI server gets different data from IE than Mozilla
        >> Firefox.
        >>
        >> // For Mozilla, req = new XMLHttpRequest( );
        >> // For IE req = new ActiveXObject(" Microsoft.XMLHT TP");
        >> req.onreadystat echange = requestHandler ; // function to handle
        >> async response
        >> req.open('POST' , myURL, true); // use POST[/color]
        >
        > First make the open call, then set the onreadystatecha nge handler and then
        > before you send the data set the HTTP request header e.g.
        > if (typeof req.setRequestH eader != 'undefined') {
        > req.setRequestH eader('Content-Type',
        > 'application/x-www-form-urlencoded');
        > }
        >[color=green]
        >> req.send('foo=1 1&bar=22') ;[/color]
        >
        > as that seems to be the content type of the data you want to post.
        >
        >
        > --
        >
        > Martin Honnen
        > http://JavaScript.FAQTs.com/[/color]


        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: POST data with XMLHttpRequest to CGI (Mozilla)

          Martin Honnen wrote:
          [color=blue]
          > Jarson wrote:[color=green]
          >> My JavaScript is trying to POST data to a CGI script (Perl) using
          >> XMLHttpRequest. My CGI server gets different data from IE than Mozilla
          >> Firefox.
          >>
          >> // For Mozilla, req = new XMLHttpRequest( );
          >> // For IE req = new ActiveXObject(" Microsoft.XMLHT TP");
          >> req.onreadystat echange = requestHandler ; // function to handle
          >> async response
          >> req.open('POST' , myURL, true); // use POST[/color]
          >
          > First make the open call, then set the onreadystatecha nge handler [...][/color]

          Does not make sense to me. Why?


          PointedEars

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: POST data with XMLHttpRequest to CGI (Mozilla)

            Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
            [color=blue]
            > Martin Honnen wrote:[/color]

            [XMLHttpRequest][color=blue][color=green]
            >> First make the open call, then set the onreadystatecha nge handler [...][/color]
            >
            > Does not make sense to me. Why?[/color]

            Why it doesn't make sense to you? Who knows :)

            Anyway: The "open" call doesn't make the connection, it merely sets up
            the request-object. The actual connection is created when the "send"
            method is called, and the ready-state will not change until this
            point. That means that it's safe to assign the handler after the call
            to "open".

            Some browsers will not honor event handlers set before the "open"
            call, as if that call completely initializes the object.

            /L
            --
            Lasse Reichstein Nielsen - lrn@hotpop.com
            DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
            'Faith without judgement merely degrades the spirit divine.'

            Comment

            Working...