六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 152|回复: 0

getAndPostExample.html

[复制链接]

升级  33.33%

88

主题

88

主题

88

主题

举人

Rank: 3Rank: 3

积分
300
 楼主| 发表于 2013-2-7 21:07:06 | 显示全部楼层 |阅读模式
例子来源:《Ajax 基础教程》 金灵 等译 这本书非常不错

getAndPostExample.html

<html><head><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 = "GetAndPostExample?";queryString = queryString + createQueryString() + "&timeStamp=" + new Date().getTime();xmlHttp.onreadystatechange = handleStateChange;xmlHttp.open("GET", queryString, true);xmlHttp.send(null);}function doRequestUsingPOST() {createXMLHttpRequest();var url = "GetAndPostExample?timeStamp=" + new Date().getTime();var queryString = createQueryString();xmlHttp.open("POST", url, true);xmlHttp.onreadystatechange = handleStateChange;xmlHttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;");xmlHttp.send(queryString);}function handleStateChange() {if(xmlHttp.readyState == 4) {if(xmlHttp.status == 200) {parseResults();}}}function parseResults() {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 action="#" ><h1>Enter your first name, middle name, and birthday:</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 parameters using GET"  /><br /><br /><input type="button" value="Send parametters using POST"  /></form><br /><h2>Server Response:</h2><div id="serverResponse"></div></body></html>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表