Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post

    post vs get methods

    Code:
    function getit(aid, aurl){
    	var req = createRequest();
    	if(req){
    		req.onreadystatechange = function(){
    			var c = document.getElementById(aid);
    			if(req.readyState){
    				if(req.readyState == 4){
    					if(req.status == 200){
    						c.innerHTML = req.responseText;
    					}
    				}
    			}
    		}
    	req.open('GET', aurl, true);
    	req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	req.send(null);
    	}
    }
    function bodyOnload(){
    fillLog1();
    fillLog2();
    fillLog3();
    fillLog4();
    fillLog5();
    }
    function fillLog1(){
    	getit('game_login_0','login.php?game=game1');
    }
    function fillLog2(){
    	getit('game_login_1','login.php?game=game2');
    }
    function fillLog3(){
    	getit('game_login_2','login.php?game=game3');
    }
    function fillLog4(){
    	getit('game_login_3','login.php?game=game4');
    }
    function fillLog5(){
    	getit('game_login_4','login.php?game=game5');
    }
    How would this work in POST method?
    Compare bible texts (and other tools):
    TheWheelofGod

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by gilgalbiblewhee View Post
    Code:
    function getit(aid, aurl){
    	var req = createRequest();
    	if(req){
    		...
    		}
    	req.open('GET', aurl, true);
    	req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	req.send(null);
    	}
    }
    How would this work in POST method?
    by changing GET to POST
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by shyam View Post
    by changing GET to POST
    Never realized that!

    What I mean is, if I have a form with fields then the url is used for the GET method by AJAX like this:
    login.php?game=game1

    But how different is POST?
    it's going to be written :
    login.php
    right?
    If so, how a I going to get game=game1?
    Last edited by gilgalbiblewhee; 10-24-2008 at 11:54 PM.
    Compare bible texts (and other tools):
    TheWheelofGod

  • #4
    New Coder
    Join Date
    Mar 2007
    Location
    italy
    Posts
    94
    Thanks
    1
    Thanked 1 Time in 1 Post
    Quote Originally Posted by gilgalbiblewhee View Post
    Never realized that!

    What I mean is, if I have a form with fields then the url is used for the GET method by AJAX like this:
    login.php?game=game1

    But how different is POST?
    it's going to be written :
    login.php
    right?
    If so, how a I going to get game=game1?
    Primarily, you should divide the url from the parameters:

    Code:
    url = "login.php";
    parameters = "?game=game1";
    Then you can use POST this way
    Code:
    	req.open('POST', url, true);
    	req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	req.send(parameters);
    	}
    this way the parameters are sent in the body of the request and are hidden from the view in the url

  • #5
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by carlitos_way View Post
    Primarily, you should divide the url from the parameters:

    Code:
    url = "login.php";
    parameters = "?game=game1";
    Then you can use POST this way
    Code:
    	req.open('POST', url, true);
    	req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	req.send(parameters);
    	}
    this way the parameters are sent in the body of the request and are hidden from the view in the url
    Ok. Thanks. First time I'm using POST.
    Compare bible texts (and other tools):
    TheWheelofGod

  • #6
    Senior Coder A1ien51's Avatar
    Join Date
    Jun 2002
    Location
    Between DC and Baltimore In a Cave
    Posts
    2,717
    Thanks
    1
    Thanked 94 Times in 88 Posts
    gilgalbiblewhee example is not 100% correct since it should not have a ? in the post.

    You also should be setting these headers when you are going to be making a post:

    Code:
    req.open('POST', url, true);
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.setRequestHeader("Content-length", params.length);
    req.setRequestHeader("Connection", "close");
    req.onreadystatechange = fooCallback;
    req.send(params);
    Eric
    Tech Author [Ajax In Action, JavaScript: Visual Blueprint]

  • Users who have thanked A1ien51 for this post:

    oesxyl (10-28-2008)

  • #7
    Regular Coder
    Join Date
    Mar 2005
    Posts
    735
    Thanks
    4
    Thanked 1 Time in 1 Post
    Quote Originally Posted by A1ien51 View Post
    gilgalbiblewhee example is not 100% correct since it should not have a ? in the post.

    You also should be setting these headers when you are going to be making a post:

    Code:
    req.open('POST', url, true);
    req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    req.setRequestHeader("Content-length", params.length);
    req.setRequestHeader("Connection", "close");
    req.onreadystatechange = fooCallback;
    req.send(params);
    Eric
    Like this?
    Code:
    // JavaScript Document
    function createRequest(){
      var req = false;
      try {
        req = new ActiveXObject('Msxml2.XMLHTTP');
      } catch (e2) {
        try {
          req = new ActiveXObject('Microsoft.XMLHTTP');
        } catch (e3) {
          try {
    	req = new XMLHttpRequest();
          } catch (e1) {
    	req = false;
          }
        }
      }
      return req;
    }
    function getit(aid, aurl){
    	var req = createRequest();
    	if(req){
    		req.onreadystatechange = function(){
    			var c = document.getElementById(aid);
    			if(req.readyState){
    				if(req.readyState == 4){
    					if(req.status == 200){
    						c.innerHTML = req.responseText;
    					}
    				}
    			}
    		}
    		var params = "?game=" + document.getElementById('game_id').value;
    	/*	
    	req.open('GET', aurl, true);
    	req.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
    	req.send(null);
    	*/
    	//http://www.codingforums.com/showthread.php?p=743212#post743212
    	req.open('POST', aurl, true);
    	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    	req.setRequestHeader("Content-length", params.length);
    	req.setRequestHeader("Connection", "close");
    	req.onreadystatechange = fooCallback;
    	req.send(params);
    	}
    }
    function bodyOnload(){
    fillLog1();
    fillLog2();
    fillLog3();
    fillLog4();
    fillLog5();
    }
    function fillLog1(){
    	getit('game_login_0','login.php');
    }
    function fillLog2(){
    	getit('game_login_1','login.php');
    }
    function fillLog3(){
    	getit('game_login_2','login.php');
    }
    function fillLog4(){
    	getit('game_login_3','login.php');
    }
    function fillLog5(){
    	getit('game_login_4','login.php');
    }
    Last edited by gilgalbiblewhee; 10-28-2008 at 10:28 PM.
    Compare bible texts (and other tools):
    TheWheelofGod


  •  

    Posting Permissions

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