发送请求参数GET POST

getandpostexample.html
CODE:


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

<html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;>

<head>

<meta http-equiv=&#34;Content-Type&#34; content=&#34;text/html; charset=utf-8&#34; />

<title>无标题文件</title>

<script type=&#34;text/javascript&#34;>

var xmlHttp;



function cr&#101;ateXMLHttpRequest(){

      if (window.ActiveXObject) {

              xmlHttp = new ActiveXObject (&#34;Microsoft.XMLHTTP&#34;);

      }

      else if (window.XMLHttpRequest) {

           xmlHttp = new XMLHttpRequest () ;

      }

}



function cr&#101;ateQueryString() {

     var firstName = document.getElementById(&#34;firstName&#34;).value;

   var middleName = document.getElementById(&#34;middleName&#34;).value;

   var birthday = document.getElementById(&#34;birthday&#34;).value;

   

   var queryString = &#34;firstName=&#34; + firstName + &#34;&middleName=&#34; + middleName + &#34;&birthday=&#34; + birthday;

   

   return queryString;

}



function doRequestUsingGET(){

     cr&#101;ateXMLHttpRequest();

   

   var queryString = &#34;getandpostexample.php?&#34;;

   queryString = queryString + cr&#101;ateQueryString() + &#34;&timeStamp=&#34; + new Date().getTime();

   xmlHttp.onreadystatechange = handleStateChange;

   xmlHttp.open(&#34;GET&#34;, queryString, true);

   xmlHttp.send(null);

}



function doRequestUsingPOST(){

     cr&#101;ateXMLHttpRequest();

   

   var url = &#34;getandpostexample.php?timeStamp&#34; + new Date().getTime();

   var queryString = cr&#101;ateQueryString();

   

   xmlHttp.open(&#34;POST&#34;, url, true);

   xmlHttp.onreadystatechange = handleStateChange;

   xmlHttp.setRequestHeader(&#34;Content-Type&#34;, &#34;application/x-www-form-urlencoded;&#34;);

   xmlHttp.send(queryString);

}



function handleStateChange() {

     if (xmlHttp.readyState == 4) {

            if (xmlHttp.status == 200) {

        parseResults();

            }

     }

}



function parseResults() {

     var responseDiv = document.getElementById(&#34;serverResponse&#34;);

   if (responseDiv.hasChildNodes()) {

          responseDiv.removeChild(responseDiv.childNodes[0]);

  }

  var responseText = document.cr&#101;ateTextNode(xmlHttp.responseText);

  responseDiv.appendChild(responseText);



   

</script>

</head>



<body>

<h1><strong>Enter your FirstName , middleName , and birthday:</strong></h1>

<table width=&#34;400&#34; border=&#34;0&#34; cellspacing=&#34;0&#34; cellpadding=&#34;0&#34;>

  <tr>

    <td width=&#34;115&#34; height=&#34;40&#34;>First name </td>

    <td width=&#34;285&#34;><input name=&#34;firstName&#34; type=&#34;text&#34; id=&#34;firstName&#34; /></td>

  </tr>

  <tr>

    <td height=&#34;40&#34;>Middle name </td>

    <td><input name=&#34;middleName&#34; type=&#34;text&#34; id=&#34;middleName&#34; /></td>

  </tr>

  <tr>

    <td height=&#34;40&#34;>Birthday</td>

    <td><input name=&#34;birthday&#34; type=&#34;text&#34; id=&#34;birthday&#34; /></td>

  </tr>

</table>

<h1>

  <input type=&#34;button&#34; name=&#34;Submit&#34; value=&#34;Send parameters using GET &#34; onclick=&#34;doRequestUsingGET();&#34; />

  


  


  <input type=&#34;button&#34; name=&#34;Submit2&#34; value=&#34;Send parameters using POST&#34; onclick=&#34;doRequestUsingPOST();&#34; />

  


  


Server Response: </h1>

<div id=&#34;serverResponse&#34;></div>

<p> </p>

</body>



</html>



getandpostexample.php
CODE:


<?

echo &#34;Hello  &#34;;

if ($_POST['firstName'] == &#34;&#34;)

  
{ echo $_GET['firstName'].&#34; &#34;; }

    
else { echo $_POST['firstName'].&#34; &#34;; }

if ($_POST['middleName'] == &#34;&#34;)

  
{ echo $_GET['middleName'].&#34; &#34;; }

    
else { echo $_POST['middleName'].&#34; &#34;; }

echo &#34;. Your Birthday is &#34;;

if ($_POST['birthday'] == &#34;&#34;)

  
{ echo $_GET['birthday'].&#34; &#34;; }

    
else { echo $_POST['birthday'].&#34; &#34;; }

?>



为什么要把时间戳追加到URL当中
在某些情况下,有些浏览器会把多个XHR请求的结果缓存在同一个URL当中,如果对每个请求的响应不同,就会带来不好的结果,把当前时间戳追加到URL的最后,能确保URL的准确避
引用通告地址: http://www.anycc.com/blog/trackback.php?tbID=66&extra=eb61ac
标签:  AJAX
评论: 0 | 引用: 0 | 阅读: 1424
发表评论
昵 称: 密 码:
网 址: 邮 箱:
验证码: 验证码图片 选 项:
头 像:
内 容: