您好,欢迎访问一九零五行业门户网

ajax中get和post的说明及使用与区别_javascript技巧

以前没怎么仔细的研究过ajax,只是用到了就直接拿过来用,发现了问题再找解决方法.以下是我在找解决问题的过程中的一点小小的总结.
一.谈ajax的get和post的区别
get方式:
用get方式可传送简单数据,但大小一般限制在1kb下,数据追加到url中发送(http的header传送),也就是说,浏览器将各个表单字段元素及其数据按照url参数的格式附加在请求行中的资源路径后面。另外最重要的一点是,它会被客户端的浏览器缓存起来,那么,别人就可以从浏览器的历史记录中,读取到此客户的数据,比如帐号和密码等。因此,在某些情况下,get方法会带来严重的安全性问题。
post方式:
当使用post方式时,浏览器把各表单字段元素及其数据作为http消息的实体内容发送给web服务器,而不是作为url地址的参数进行传递,使用post方式传递的数据量要比使用get方式传送的数据量大的多。
总之,get方式传送数据量小,处理效率高,安全性低,会被缓存,而post反之。
使用get方式需要注意:
1 对于get请求(或凡涉及到url传递参数的),被传递的参数都要先经encodeuricomponent方法处理.例:var url = update.php?username= +encodeuricomponent(username) + &content= +encodeuricomponent
(content)+&id=1 ;
使用post方式需注意:
1.设置header的context-type为application/x-www-form-urlencode确保服务器知道实体中有参数变量.通常使用xmlhttprequest对象的setrequestheader(context-type,application/x-www-form-urlencoded;)。例:
xmlhttp.setrequestheader(content-type,application/x-www-form-urlencoded);
2.参数是名/值一一对应的键值对,每对值用&号隔开.如 var name=abc&sex=man&age=18,注意var name=update.php?
abc&sex=man&age=18以及var name=?abc&sex=man&age=18的写法都是错误的;
3.参数在send(参数)方法中发送,例: xmlhttp.send(name); 如果是get方式,直接 xmlhttp.send(null);
4.服务器端请求参数区分get与post。如果是get方式则$username = $_get[username]; 如果是post方式,则$username = $_post[username];
post和get 方法有如下区别:
1.post传输数据时,不需要在url中显示出来,而get方法要在url中显示。
2.post传输的数据量大,可以达到2m,而get方法由于受到url长度的限制,只能传递大约1024字节.
3.post顾名思义,就是为了将数据传送到服务器段,get就是为了从服务器段取得数据.而get之所以也能传送数据,只是用来设计告诉服务器,你到底需要什么样的数据.post的信息作为http请求的内容,而get是在http头部传输的。
get 方法用request.querystring[strname]接收
post 方法用request.form[strname] 接收
注意:
虽然两种提交方式可以统一用request(strname)来获取提交数据,但是这样对程序效率有影响,不推荐使用。
一般来说,尽量避免使用get方式提交表单,因为有可能会导致安全问题
ajax乱码问题
产生乱码的原因:
1、xtmlhttp 返回的数据默认的字符编码是utf-8,如果客户端页面是gb2312或者其它编码数据就会产生乱码
2、post方法提交数据默认的字符编码是utf-8,如果服务器端是gb2312或其他编码数据就会产生乱码
解决办法有:
1、若客户端是gb2312编码,则在服务器指定输出流编码
2、服务器端和客户端都使用utf-8编码
gb2312:header('content-type:text/html;charset=gb2312');
utf8:header('content-type:text/html;charset=utf-8');
注意:如果你已经按上面的方法做了,还是返回乱码的话,检查你的方式是否为get,对于get请求(或凡涉及到url传递参数的),被传递的参数都要先经encodeuricomponent方法处理.如果没有用encodeuricomponent处理的话,也会产生乱码.
下边是我找到的一个例子,因为写的不错就贴在这里了,自己写的比较简单,也不是很规范还是参考人家写的好了,呵呵!
复制代码 代码如下:
var http_request = false;
function makepostrequest(url, parameters) {
http_request = false;
if (window.xmlhttprequest) { // mozilla, safari,...
http_request = new xmlhttprequest();
if (http_request.overridemimetype) {
// set type accordingly to anticipated content type
//http_request.overridemimetype('text/xml');
http_request.overridemimetype('text/html');
}
} else if (window.activexobject) { // ie
try {
http_request = new activexobject(msxml2.xmlhttp);
} catch (e) {
try {
http_request = new activexobject(microsoft.xmlhttp);
} catch (e) {}
}
}
if (!http_request) {
alert('cannot create xmlhttp instance');
return false;
}
http_request.onreadystatechange = alertcontents;
http_request.open('post', url, true);
http_request.setrequestheader(content-type, application/x-www-form-urlencoded);
http_request.setrequestheader(content-length, parameters.length);
http_request.setrequestheader(connection, close);
http_request.send(parameters);
}
function alertcontents() {
if (http_request.readystate == 4) {
if (http_request.status == 200) {
//alert(http_request.responsetext);
result = http_request.responsetext;
document.getelementbyid('myspan').innerhtml = result;
} else {
alert('there was a problem with the request.');
}
}
}
function get(obj) {
var poststr = mytextarea1= + encodeuri( document.getelementbyid(mytextarea1).value ) +
&mytextarea2= + encodeuri( document.getelementbyid(mytextarea2).value );
makepostrequest('post.php', poststr);
}
post.php
一个超大文本框textarea里面有大量数据,ajax通过url请求service返回结果,url里面包含了各种参数,当然也包含之前的超大文本框的内容。 之前开发的时候一直用firefox在调试,4000长度的字符串在textarea里面通过url请求都是没有问题。 提交给测试的时候问题来了,测试人员在ie下面发现问题,textarea里面字符长度超过2000(大概数据)时,会报js错误,ajax没有返回值给前台。 看原先代码:
复制代码 代码如下:
function getjsondata(url)
{
var ajax = common.createxmlhttprequest();
ajax.open(get,url,false);
ajax.send(null);
try
{
eval(var s = +ajax.responsetext);
return s;
}
catch(e)
{
return null;
}
}
function getdata(){
var url=blacklistservice.do?datas=+datasvalue;
var result = getjsondata(url);
}
网上google发现解决办法: 修改使用的xmlhttp的请求为post,并且把参数和url分离出来提交。 修改后代码如下:
复制代码 代码如下:
function getjsondata(url,para)
{
var ajax = common.createxmlhttprequest();
ajax.open(post,url,false);
ajax.setrequestheader('content-type','application/x-www-form-urlencoded');
ajax.send(para);
try
{
eval(var s = +ajax.responsetext);
return s;
}
catch(e)
{
return null;
}
}
function getdata(){
var url=blacklistservice.do;
var para=datas=+datasvalue;
var result = getjsondata(url,para);
}
================================
ajax中的get和post两种请求方式的异同2008年10月04日 星期六 下午 02:37分析两种提交方式的异同ajax中我们经常用到get和post请求.那么什么时候用get请求,什么时候用post方式请求呢? 在做回答前我们首先要了解get和post的区别.
1、 get是把参数数据队列加到提交表单的action属性所指的url中,值和表单内各个字段一一对应,在url中可以看到。post是通过http post机制,将表单内各个字段与其内容放置在html header内一起传送到action属性所指的url地址。用户看不到这个过程。
2、 对于get方式,服务器端用request.querystring获取变量的值,对于post方式,服务器端用request.form获取提交的数据。两种方式的参数都可以用request来获得。
3、get传送的数据量较小,不能大于2kb。post传送的数据量较大,一般被默认为不受限制。但理论上,因服务器的不同而异.
4、get安全性非常低,post安全性较高。
5、 跟是一样的,也就是说,method为get时action页面后边带的参数列表会被忽视;而跟是不一样的。另外 get请求有如下特性:它会将数据添加到url中,通过这种方式传递到服务器,通常利用一个问号?代表url地址的结尾与数据参数的开端,后面的参数每一个数据参数以“名称=值”的形式出现,参数与参数之间利用一个连接符&来区分。 post请求有如下特性:数据是放在http主体中的,其组织方式不只一种,有&连接方式,也有分割符方式,可隐藏参数,传递大批数据,比较方便。通过以上的说明,现在我们大致了解了什么时候用get什么时候用post方式了吧,对!当我们在提交表单的时候我们通常用post方式,当我们要传送一个较大的数据文件时,需要用post。当传递的值只需用参数方式(这个值不大于2kb)的时候,用get方式即可。现在我们再看看通过url发送请求时,get方式和post方式的区别。用下面的例子可以很容易的看到同样的数据通过get和post来发送的区别, 发送的数据是 username=张三 :
复制代码 代码如下:
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 方式:
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
区别就是一个在 url 请求里面附带了表单参数和值, 一个是在 http 请求的消息实体中。比较一下上面的两段文字, 我们会发现 get 方式把表单内容放在前面的请求头中, 而 post 则把这些内容放在请求的主体中了, 同时 post 中把请求的 content-type 头设置为 application/x-www-form-urlencoded. 而发送的正文都是一样的, 可以这样来构造一个表单提交正文: encodeuricomponent(arg1)=encodeuricomponent(value1)&encodeuricomponent(arg2)=encodeuricomponent(value2)&.....注: encodeuricomponent 返回一个包含了 charstring 内容的新的 string 对象(unicode 格式), 所有空格、标点、重音符号以及其他非 ascii 字符都用 %xx 编码代替,其中 xx 等于表示该字符的十六进制数。 例如,空格返回的是 %20 。 字符的值大于 255 的用 %uxxxx 格式存储。参见 javascript 的 encodeuricomponent() 方法.在了解了上面的内容后我们现在用ajax的xmlhttprequest对象向服务器分别用get和post方式发送一些数据。
get 方式
复制代码 代码如下:
var postcontent =name= + encodeuricomponent(xiaocheng) + &email= + encodeuricomponent(xiaochengf_21@yahoo.com.cn);
xmlhttp.open(get, somepage + ? + postcontent, true);
xmlhttp.send(null);
post 方式
复制代码 代码如下:
var postcontent =name= + encodeuricomponent(xiaocheng) + &email= + encodeuricomponent(xiaochengf_21@yahoo.com.cn);
xmlhttp.open(post, somepage, true);
xmlhttp.setrequestheader(content-type, application/x-www-form-urlencoded);
//xmlhttp.setrequestheader(content-type, text/xml); //如果发送的是一个xml文件
xmlhttp.send(postcontent);
ajax的post方法的使用.
刚开始学ajax,看到很多网上的代码都用get方法提交参数,tomcat默认iso编码实在是让人头痛 ,对付乱码我都是用过滤器做字符编码过滤的,get方法过滤器监听不到,所以我一直喜欢使用post方法,下面对ajax get和post方法做一对比
get:
复制代码 代码如下:
post:
复制代码 代码如下:
可以发现,get方法根据地址栏解析参数,post根据sendrequestpost(url,param);中的param字符串解析参数,重要的是post方法中需要添加在open();方法后需要添加xmlhttprequest.setrequestheader(content-type,application/x-www-form-urlencoded);这句代码,不知道为什么,初学,加了就能传递参数了,日后研究。
其它类似信息

推荐信息