关于get和post

来源:互联网 发布:node express -e 编辑:程序博客网 时间:2024/06/06 02:54

get和post都是向服务器发送请求的类型。

get的请求:

xhr.open("get","test1.php?t="+Math.random(),true);xhr.send();


post的请求:

<pre name="code" class="html">xhr.open("post","test1.php",true);

xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded");xhr.send("fname=Bill&lname=Gates");

get使用url进行传递,传输的数据直接在url上显示,作为http头部进行传递;而post则是作为http请求的内容进行传递的。


到底是使用get还是post?

大多数情况下使用get,因为get更简单更快捷。

以下情况使用post:

1、无法使用缓存文件时(更新服务器上的文件或数据库);

2、向服务器发送大量的数据时(get有大小限制,特定的浏览器和服务器对其大小限制不同,如IE对URL的长度限制为2083字节,即2K+35;post在理论上没有大小限制);

3、发送包含未知字符的用户输入时(post比get更加安全更加可靠)


0 0
原创粉丝点击