GET vs POST

来源:互联网 发布:xampp查看php版本 编辑:程序博客网 时间:2024/05/16 05:56

尽管load()方法可以实现GET 和POST两种方式,但很多时候开发者还是希望能够制定发送方式,并且处理服务器返回的值。

jQuery 提供了$.get() 和$.post() 两种方法,分别针对这两种请求方式,语法如下:

$.get(url,[data],[callback])

$.post(url,[data],[callback],[type])

url    为请求地址,data为请求数据的列表,是可选参数,callback为请求成功后的回调函数,该函数接受两个参数,第一个参数为服务器返回

的数据,第二个参数为服务器的状态,是可选参数。$.post()中的type 为请求数据的类型,可以是HTML、XML、JSON等。

 jQuery中GET 和POST比较:

        <script type="text/javascript" language="javascript">        function createQueryString()        {            var firstName = encodeURI($("#firstName").val());            var birthday = encodeURI($("#birthday").val());            //组合成对象的形式            var queryString = { firstName: firstName, birthday: birthday };            return queryString;        }        function doRequestUsingGET()        {            $.get("test.aspx", createQueryString(),            //发送GET请求    function (data)    { $("#serverResponse").html(decodeURI(data)) });        }        function doRequestUsingPOST()        {            $.post("test.aspx", createQueryString(),            //发送GET请求    function (data)    { $("#serverResponse").html(decodeURI(data)) }            );        }     </script>
服务器端代码如下:
if (Request.HttpMethod == "POST")            {                Response.Write("POST:" + Request["firstName"] + ",your birthday is " + Request["birthday"]);            }            else if (Request.HttpMethod == "GET")            {                Response.Write("GET:" + Request["firstName"] + ",your birthday is " + Request["birthday"]);            }



原创粉丝点击