我们在使用Ajax时,当我们向服务器发送数据时,我们可以采用Get方式请求服务器,也可以使用Post方式请求服务器.那么,我们什么时候该采用Get方式,什么时候该采用Post方式呢?
Get请求和Post请求的区别
1.使用Get请求时,参数在URL中显示,而使用Post方式,则不会显示出来
2.使用Get请求发送数据量小,Post请求发送数据量大
例子:页面的HTML代码:
<html xmlns="http://www.w3.org/1999/xhtml"><head> <title></title> <style type="text/css"> * { margin:8px; } </style></head><body> <label for="txt_username"> 姓名:</label> <input type="text" id="txt_username" /> <br /> <label for="txt_age"> 年龄:</label> <input type="text" id="txt_age" /> <br /> <input type="button" value="GET" id="btn_get" onclick="btn_get_click();" /> <input type="button" value="POST" id="btn_post" onclick="btn_post_click();" /> <div id="result"> </div></body></html> |
区别:客户端脚本代码:Get请求
function btn_get_click() { var xmlHttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); var username = document.getElementById("txt_username").value; var age = document.getElementById("txt_age").value; //添加参数,以求每次访问不同的url,以避免缓存问题 xmlHttp.open("get", "Server.aspx?username=" + encodeURIComponent(username) + "&age=" + encodeURIComponent(age) + "&random=" + Math.random()); xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { document.getElementById("result").innerHTML = xmlHttp.responseText; } } //发送请求,参数为null xmlHttp.send(null);} | |
Post请求
function btn_post_click() { var xmlHttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP"); var username = document.getElementById("txt_username").value; var age = document.getElementById("txt_age").value; var data = "username=" + encodeURIComponent(username) + "&age=" + encodeURIComponent(age); //不用担心缓存问题 xmlHttp.open("post", "Server.aspx", true); //必须设置,否则服务器端收不到参数 xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); xmlHttp.onreadystatechange = function () { if (xmlHttp.readyState == 4 && xmlHttp.status == 200) { document.getElementById("result").innerHTML = xmlHttp.responseText; } } //发送请求,要data数据 xmlHttp.send(data);} |
|
区别:
1.get请求需注意缓存问题,post请求不需担心这个问题
2.post请求必须设置Content-Type值为application/x-form-www-urlencoded
3.发送请求时,因为get请求的参数都在url里,所以send函数发送的参数为null,而post请求在使用send方法时,却需赋予其参数
对于客户端代码中都请求的server.aspx,我们来看server.aspx.cs中的代码:
protected void Page_Load(object sender, EventArgs e)
{
string username = string.Empty;
int age = 0;
if (Request.HttpMethod.ToUpper().Equals("GET"))
{
username = Request.QueryString["username"];
age = int.Parse(Request.QueryString["age"]);
}
else
{
username = Request.Form["username"];
age = int.Parse(Request.Form["age"]);
}
Response.Clear();
Response.Write("姓名:'" + username + "'<br/>年龄:" + age + "<br/>时间:'" + DateTime.Now.ToString() + "'");
Response.End();
}
此处,我们发现了get请求和post请求在服务器端的区别:
在客户端使用get请求时,服务器端使用Request.QueryString来获取参数,而客户端使用post请求时,服务器端使用Request.Form来获取参数.
关于服务器端获取数据,我们还可以使用一个通用的获取参数的方式即Request["username"],但是此方法存在一个问题,我们随后来讲.
下面,我们使用HttpWatch来看下,当使用get和post方式发送请求时,客户端究竟发送了什么,收到了什么.
对于get请求和post请求中的时间差,请不要在意,因为是在不同时间按下的get按钮和post按钮.
从请求的url可以看出,get请求是带着参数的,post请求的url则不带.
从cache可以看出,get请求在发送后,即被缓存,而post请求时 never cached.
因为get请求的参数是在url中的,所以Query String中是有值的.而post请求则没有.
在Post Data里,因为get请求的字符串是在url中附带的,所以Post Data中无数据.
从服务器获取的内容都是一致的.
http://www.cnblogs.com/oneword/archive/2011/06/06/2073533.html
分享到:
相关推荐
Ajax中Get请求和Post请求的区别 简单例子 何时使用Get请求,何时使用Post请求
学习ajax技术的时候,很多同学对于ajax中get与post请求总是疑问不解
Ajax Get、Post请求 Ajax Get、Post请求 Ajax Get、Post请求
AJAX POST与get提交数据的差别,喜欢了解详细内容的朋友可以下载学习!
Android Webview虽然提供了页面加载及资源请求的钩子,但是对于h5的ajax请求并没有提供干涉的接口,这意味着我们不能在webview中干涉javascript发起的http请求,而有时候我们确实需要能够截获ajax请求并实现一些功能...
比如我们经常这样写一个ajax的post请求: $.ajax({ type: "post", // post or get contentType:"application/json;charset=utf-8", data: “requestTag=”+tag+"&content="+content, //请求参数 url: "po
Java 模拟Ajax POST GET 提交代码,实测很好用。
服务端模拟ajax,支持发起GET、POST、PUT、DELETE请求
图书馆页面添加删除修改
Ajax的小封装对ajax的get请求的小封装.
AJAX发送请求与接受,入门知识很好很基出,POSTANDGET
一般我们在开发中经常会用到Ajax请求,异步发送请求,然后获取我们想要的数据,在Ajax中使用Get请求数据不会有页面缓存的问题,而使用POST请求可是有时候页面会缓存我们提交的信息,导致我们发送的异步请求不能...
ajax简易模型 兼容多个浏览器(ff ie ...) get post请求
前言:博主之前有篇文章是快速入门Ajax ,主要是利用Ajax做简单的get请求,今天给大家分享一篇利用Ajax提交post请求,以及使用post时需要注意的地方,还是以案例的方式告诉大家。 案例: 注册表单 文件结构图: 06-...
get如果请求url没有变化,取出缓存,提高效率;请求会缓存到浏览器中,可以通过历史记录查看用户信息,安全性低; post传送变化的数据显示,变动性访问; get通过url地址传送数据,数据量不能超过1024byte; post...
具体描述ajax的获取,ajax属性,参数设置,编程步骤,缓存问题,get/post乱码问题,以及应用场景的阐述 onreadystatechange 绑订事件处理函数(处理readystatechange事件)。 注:当readyState属性值发生了任何的改变...
function Ajax(url,method,parameter,onSuccess,onError,onLoad){ this.xmlHttp=null; this.url=url; this.method=method; this.parameter=parameter; this.success=onSuccess?onSuccess:null; this.error=on...
创建本地服务器,包含简易JSON链,用ajax发送get,post请求以查找本地服务器中的JSON链
Ajax请求session超时处理流程 java服务器端处理: SessionValidateFilter中修改: if (ServerInfo.isAjax(request)) { request.setAttribute("statusCode", 301); request.setAttribute("message", "Session ...