AJAX 学习笔记(7) 发送请求参数

本文介绍了一种在Ajax应用中使用GET和POST方法发送请求的具体实现方式,并解释了为何要将时间戳追加到URL末尾以防止浏览器缓存请求结果。

在大多数情况下,向服务器发送一个请求而没有任何参数是没有意义的.

Ajax_Get_Post.aspx (原文件名:getAdnPostExample.html,本人想在.Net上改写)

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ajax_Get_Post.aspx.cs" Inherits="Ajax_Get_Post" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Sending Request Data Using Get and Post</title>
    <script type="text/javascript">
        var xmlHttp;
   
        function createXmlHttpRequest(){
            if(window.ActiveXObject){
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
        else if(window.XmlHttpRequest){
            xmlHttp = new XmlHttpRequest;
            }
       }
       
        function createQueryString(){
            var firstName = document.getElementById("firstName").value;
            var middleName = document.getElementById("middleName").value;
            var birthday = document.getElementById("birthday").value;
           
            var queryString = "firstName=" + firstName + "&middleName=" + middleName + "&birthday=" + birthday;
            return queryString;       
        }
       
        function doRequestUsingGET(){
            createXmlHttpRequest();           
            var queryString = "Ajax_Get_Post.aspx?";           
            queryString  = queryString + createQueryString() + "&timeStamp=" + new Date().getTime();
            xmlHttp.onreadyStateChange = handleStateChange;
            xmlHttp.open("GET", queryString, true);
            xmlHttp.send(null);           
        }
       
        function doRequestUsingPOST(){
            createXmlHttpRequest();
            var url = "Ajax_Get_Post.aspx?timeStamp=" + new Date().getTime();
            var queryString = createQueryString();
           
            xmlHttp.onreadyStateChange = handleStateChange;
            xmlHttp.open("POST", url, true);
            xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
            xmlHttp.send(queryString);                 
        }
       
        function handleStateChange(){
        if(xmlHttp.readyState == 4){
            if(xmlHttp.status == 200){               
                parseReqults();
                }
            }  
        }
       
        function parseReqults(){
            var responseDIV = document.getElementById("serverResponse");
            if(responseDIV.hasChildNodes()){
                responseDIV.removeChild(responseDIV.childNodes[0]);
            }
           
            var responseText = document.createTextNode(xmlHttp.responseText);
            responseDIV.appendChild(responseText);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <h1>Enter Your First Name, Middle Name and Birthdy</h1>
    <table>
    <tbody>
    <tr>
    <td>First Name:</td>
    <td><input type="text" id="firstName" /></td>
    </tr>
    <tr>
    <td>Middle Name:</td>
    <td><input type="text" id="middleName" /></td>
    </tr>
    <tr>
    <td>Birthday:</td>
    <td><input type="text" id="birthday" /></td>
    </tr>
    </tbody>
    </table>
    <input  type="button" value="Send paramenters using GET" onclick="doRequestUsingGET();" />
    <br />
    <input  type="button" value="Send paramenters using POST" onclick="doRequestUsingPOST();" />
   
    <br />
    <h2>Server Response:</h2>
    <div id="serverResponse"></div>
    </form>
</body>
</html>

请问下面这段Java Servlet 代码怎么改写,才能在Ajax_Get_Post.aspx 运行起来??


                 //Process the request in method processRequest
                  processRequest(request, response, "POST");
                }
         }

请问上面这段Java Servlet 代码怎么改写,才能在Ajax_Get_Post.aspx 运行起来??

为什么要把时间戳追加到目标URL?

在某些情况下,有些浏览器会把多个XMLHttpRequest请求的结果缓存在同一个UEL。如果对每个请求的响应不同,这就会带来不好的结果。把当前时间戳追加到URL的最后,就能确保URL的惟一性,从面避免浏览器缓存结果。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值