Introduction to Ajax get and post differences

Source: Internet
Author: User
Tags html header http post

1, get is to add the parameter data queue to submit the form of the action attribute refers to the URL, the value and the form of each field one by one corresponding, in the URL can be seen. Post is the HTTP post mechanism that places the fields in the form and their contents in the HTML header to the URL address that the action attribute refers to. This process is not visible to the user.

2, for Get way, server end uses Request.QueryString to obtain variable value, for post way, server end uses Request.Form to obtain the submitted data. The parameters of both methods can be obtained by request.

3. The amount of data transferred by get is less than 2KB. Post transfers have a large amount of data, which is generally default to unrestricted. In theory, however, it varies depending on the server.

4, get security is very low, post security is high.

5, <form method= "get" action= "A.asp?b=b" > with <form method= "get" action= "a.asp" > is the same, that is to say, The list of arguments behind the action page when method is get is ignored, while <form method= "post" action= "A.asp?b=b" > and <form method= "Post action=" A.asp "> is not the same

Learn Ajax post, program effect see Ajax Post pass value.

The code is as follows Copy Code

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>ajax Post Value </title>

<script language= "JavaScript" >
function Saveuserinfo ()
{
Get the Accept return information layer
var msg = document.getElementById ("msg");

Get form objects and user information values
var f = document.user_info;
var userName = F.user_name.value;
var userage = F.user_age.value;
var usersex = F.user_sex.value;

Receive the URL address for the form
var url = "ajax_output1.php";

The value of the post is required to join each variable by &
var poststr = "User_name=" + userName + "&user_age=" + userage + "&user_sex=" + usersex;

Instantiating Ajax
var ajax = Initajax ();


var Ajax = false;
Start initializing the XMLHttpRequest object
if (window. XMLHttpRequest)
{//mozilla Browser
Ajax = new XMLHttpRequest ();
if (Ajax.overridemimetype)
{//Set MIME category
Ajax.overridemimetype ("Text/xml");
}
}
else if (window. ActiveXObject)
{//IE browser
Try
{
Ajax = new ActiveXObject ("Msxml2.xmlhttp");
}
catch (E)
{
Try
{
Ajax = new ActiveXObject ("Microsoft.XMLHTTP");
}
catch (e) {}
}
}
if (!ajax)
{///exception, failed to create object instance
Window.alert ("Cannot create XMLHttpRequest object instance.");
return false;
}

To open a connection by post
Ajax.open ("POST", url, True);

Defines the transmitted file HTTP header information
Ajax.setrequestheader ("Content-type", "application/x-www-form-urlencoded");

Send post data
Ajax.send (POSTSTR);

Get Execution status
Ajax.onreadystatechange = function ()
{
If the execution status succeeds, the return information is written to the specified layer
if (ajax.readystate = = 4 && ajax.status = 200)
{
msg.innerhtml = Ajax.responsetext;
}
}
}
</script>

<body >
<div id= "MSG" ></div>
<form name= "User_info" method= "POST" action= "" >
Name: <input type= "text" name= "user_name"/><br/>
Age: <input type= "text" name= "User_age"/><br/>
Sex: <input type= "text" name= "User_sex"/><br/>
<input type= "button" value= "Submitting form" onclick= "Saveuserinfo ()" >
</form>
</body>

Then there is the Ajax get, which is the purpose of getting the value from the server.

Program effects refer to the Ajax get pass value.

The code is as follows Copy Code

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title>ajax Get pass Value </title>

<script language= "JavaScript" >
function Saveuserinfo ()
{
Get the Accept return information layer
var msg = document.getElementById ("msg");

Get form objects and user information values
var f = document.user_info;
var userName = F.user_name.value;
var userage = F.user_age.value;
var usersex = F.user_sex.value;

Receive the URL address for the form
var url = "ajax_output2.php?" User_name= "+ userName +" &user_age= "+ userage +" &user_sex= "+ usersex;

Instantiating Ajax
var ajax = Initajax ();

var Ajax = false;
Start initializing the XMLHttpRequest object
if (window. XMLHttpRequest)
{
Mozilla Browser
Ajax = new XMLHttpRequest ();
if (Ajax.overridemimetype)
{//Set MIME category
Ajax.overridemimetype ("Text/xml");
}
}
else if (window. ActiveXObject)
{//IE browser
Try
{
Ajax = new ActiveXObject ("Msxml2.xmlhttp");
}
catch (E)
{
Try
{
Ajax = new ActiveXObject ("Microsoft.XMLHTTP");
catch (e) {}
}
}
if (!ajax)
{
exception, creating object instance failed
Window.alert ("Cannot create XMLHttpRequest object instance.");
return false;
}

To open a connection by post
Ajax.open ("Get", url, True);

Send a Get data, already assigned a value in the URL so send there just add a null parameter.
Ajax.send (NULL);

Get Execution status
Ajax.onreadystatechange = function ()
{
If the execution status succeeds, the return information is written to the specified layer
if (ajax.readystate = = 4 && ajax.status = 200)
{
msg.innerhtml = Ajax.responsetext;
}
}
}
</script>

<body>
<div id= "MSG" ></div>
<form name= "User_info" method= "POST" action= "" >
<input type= "text" name= "user_name" style= "Display:none"/>
<input type= "text" name= "User_age" style= "Display:none"/>
<input type= "text" name= "User_sex" style= "Display:none"/>
<input type= "button" value= "Get server variable" onclick= "Saveuserinfo ()" >
</form>
</body>

ajax_output2.php

The code is as follows Copy Code
?
$user _name = "Gonn";
echo $user _name;
$user _age = "24";
echo $user _age;
$user _sex = "man";
echo $user _sex;
?>



Summarize


Now let's look at the difference between get and post when sending a request through a URL. The following example makes it easy to see the difference between the same data sent via Get and post, and the data sent is Username= John:
Get way, browser type http://localhost?username= john

The code is as follows Copy Code


Get/?username=%e5%bc%a0%e4%b8%89 http/1.1
Accept:image/gif, Image/x-xbitmap, Image/jpeg, Image/pjpeg, Application/vnd.ms-powerpoint, application/vnd.ms-excel , Application/msword, */*
Accept-language:zh-cn
Accept-encoding:gzip, deflate
user-agent:mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0;. NET CLR 1.1.4322)
Host:localhost
Connection:keep-alive

POST Method:

  code is as follows copy code

post/http/ 1.1
Accept:image/gif, Image/x-xbitmap, Image/jpeg, Image/pjpeg, Application/vnd.ms-powerpoint, application/ Vnd.ms-excel, Application/msword, */*
ACCEPT-LANGUAGE:ZH-CN
content-type:application/x-www-form-urlencoded
Accept-encoding:gzip, deflate
user-agent:mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0;. NET CLR 1.1.4322)
Host:localhost
content-length:28
connection:keep-alive

username=% e5%bc%a0%e4%b8%89

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.