www.webdeveloper.com
Results 1 to 6 of 6

Thread: post instead of get

  1. #1
    Join Date
    Jan 2011
    Posts
    39

    post instead of get

    Want to get the example below to work with open("POST",url,true) instead of open("GET",url,true).
    When I try that, the program doesn't seem to do anything when I click the buttons. The last try is commented out.
    How do you get it to work with POST ?

    Code:
    <html>
       <head>
          <title>
          </title>
          <script language="javascript" type="text/javascript">
          <!--
             function f(fn,dv)
             {
                var req=false;
                if (window.XMLHttpRequest)
                {
                   req=new XMLHttpRequest();
                }
                else
                {
                   if (window.ActiveXObject)
                   {
                      req=new ActiveXObject("Microsoft.XMLHTTP");
                   }
                };
                if (req)
                {
                   var fn1=fn+"?t="+Math.random();
                   req.open("GET",fn1,true);
                   //req.open("POST",fn1,true);
                   //req.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                   req.onreadystatechange=function()
                                          {
                                             if (req.readyState==4 && req.status==200)
                                             {
                                                //var rt2=req.responseText;
                                                var rt2="<img src="+req.responseText+">";
                                                document.getElementById(dv).innerHTML=rt2;
                                                delete req;
                                                req=null;
                                             }
                                          };
                   req.send(null);
                   //req.send();
                }
                else
                {
                   alert('No req.');
                   return false;
                };
             }
          -->
          </script>
       </head>
       <body>
          <div id="dv1">
             <form>
                <input type="button" value="*" onclick="f('aa.txt','dv2');">
                <input type="submit" value="*">
             </form>
          </div>
          <div id="dv2">
          </div>
       </body>
    </html>

  2. #2
    your req.send has to have the parameters passed inside it.

    Code:
    req.open('POST", fn, true);
    req.setRequestHeader('Content-type','application/x-www-form-urlencoded');
    req.onreadystatechange=function() {
    	if (req.readyState==4 && req.status==200) {
    		document.getElementById(dv).innerHTML = '<img src='+req.responseText+'>';
    		delete req;
    		req = null;
    	}
    };
    req.send('t=' + Math.random());
    You put it in the send, not in the URL.

    Oh, and small tip, TRY to avoid making 'variables for nothing'.

  3. #3
    Join Date
    Jan 2011
    Posts
    39
    Tried that and other things but no luck. Req.readyState went to 4 and req.status was 405.

  4. #4
    Join Date
    Jan 2011
    Posts
    39
    Sorry, by accident reposted reply above.
    Last edited by neginf; 06-25-2014 at 07:58 AM.

  5. #5
    405? Method not allowed?

    Can you make a flat non-ajax method="post" form and have it work? Could be POST is blocked... could also be your server doesn't allow post and AJAX.

    Though you're passing to a txt file, which makes no sense. "aa.txt" is not something you should have as a URL to a form handler. Apache could be blocking it. What is "aa.txt"? If you change it to a PHP file does it work?

  6. #6
    Join Date
    Jan 2011
    Posts
    39
    The text file extension was the problem.
    The example works with post method now.
    Thank you very much.
    Last edited by neginf; 06-25-2014 at 11:30 AM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles