()post
http://04101334.iteye.com/blog/637695/
()get
function serializeElement(element) {
var method = element.tagName.toLowerCase();
var parameter = input(element);
if (parameter) {
var key = encodeURIComponent(parameter[0]);
if (key.length == 0) return;
if (parameter[1].constructor != Array)
parameter[1] = [parameter[1]];
var values = parameter[1];
var results = [];
for (var i=0; i<values.length; i++) {
results.push(key + '=' + encodeURIComponent(values[i]));
}
return results.join('&');
}
}
function serializeForm(formId) {
var elements = getElements(formId);
var queryComponents = new Array();
for (var i = 0; i < elements.length; i++) {
var queryComponent = serializeElement(elements[i]);
if (queryComponent)
queryComponents.push(queryComponent);
}
return queryComponents.join('&');
}
http://04101334.iteye.com/blog/637695/
() get,post 的区别
http://blog.****.net/zygsee/article/details/5264103
http://bbs.phpchina.com/thread-138396-1-1.html 一般不用get 而用post的原因
学习 Ajax 那么多的理论,这里有两个不错的 Ajax Post 与 Get 的实例,让我们去学习下吧。
先是学习 Ajax 的 Post,程序效果请参看 Ajax 的 Post 传值。
03 |
<meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" />
|
04 |
<title>Ajax Post 传值</title> |
07 |
<script language= "javascript" >
|
08 |
function saveUserInfo()
|
11 |
var msg = document.getElementById( "msg" );
|
14 |
var f = document.user_info;
|
15 |
var userName = f.user_name.value;
|
16 |
var userAge = f.user_age.value;
|
17 |
var userSex = f.user_sex.value;
|
20 |
var url = "ajax_output1.php" ;
|
22 |
//需要POST的值,把每个变量都通过&来联接
|
23 |
var postStr = "user_name=" + userName + "&user_age=" + userAge + "&user_sex=" + userSex;
|
26 |
//var ajax = InitAjax();
|
30 |
//开始初始化XMLHttpRequest对象
|
31 |
if (window.XMLHttpRequest)
|
33 |
ajax = new XMLHttpRequest();
|
34 |
if (ajax.overrideMimeType)
|
36 |
ajax.overrideMimeType( "text/xml" );
|
39 |
else if (window.ActiveXObject)
|
43 |
ajax = new ActiveXObject( "Msxml2.XMLHTTP" );
|
49 |
ajax = new ActiveXObject( "Microsoft.XMLHTTP" );
|
56 |
window.alert( "不能创建XMLHttpRequest对象实例." );
|
61 |
ajax.open( "POST" , url, true );
|
64 |
ajax.setRequestHeader( "Content-Type" , "application/x-www-form-urlencoded" );
|
70 |
ajax.onreadystatechange = function ()
|
72 |
//如果执行状态成功,那么就把返回信息写到指定的层里
|
73 |
if (ajax.readyState == 4 && ajax.status == 200)
|
75 |
msg.innerHTML = ajax.responseText;
|
83 |
<form name= "user_info" method= "post" action= "" >
|
84 |
姓名:<input type= "text" name= "user_name" /><br />
|
85 |
年龄:<input type= "text" name= "user_age" /><br />
|
86 |
性别:<input type= "text" name= "user_sex" /><br />
|
87 |
<input type= "button" value= "提交表单" onClick= "saveUserInfo()" >
|
然后是 Ajax 的 Get,用途是从服务器获取值。
程序效果请参看 Ajax 的 Get 传值。
03 |
<meta http-equiv= "Content-Type" content= "text/html; charset=utf-8" />
|
04 |
<title>Ajax Get 传值</title> |
07 |
<script language= "javascript" >
|
08 |
function saveUserInfo()
|
11 |
var msg = document.getElementById( "msg" );
|
14 |
var f = document.user_info;
|
15 |
var userName = f.user_name.value;
|
16 |
var userAge = f.user_age.value;
|
17 |
var userSex = f.user_sex.value;
|
20 |
var url = "ajax_output2.php? user_name=" + userName + "&user_age=" + userAge + "&user_sex=" + userSex;
|
23 |
//var ajax = InitAjax();
|
26 |
//开始初始化XMLHttpRequest对象
|
27 |
if (window.XMLHttpRequest)
|
30 |
ajax = new XMLHttpRequest();
|
31 |
if (ajax.overrideMimeType)
|
33 |
ajax.overrideMimeType( "text/xml" );
|
36 |
else if (window.ActiveXObject)
|
40 |
ajax = new ActiveXObject( "Msxml2.XMLHTTP" );
|
46 |
ajax = new ActiveXObject( "Microsoft.XMLHTTP" );
|
53 |
window.alert( "不能创建XMLHttpRequest对象实例." );
|
58 |
ajax.open( "GET" , url, true );
|
60 |
//发送GET数据,已经在URL中赋了值所以send那里只要加个空参.
|
64 |
ajax.onreadystatechange = function ()
|
66 |
//如果执行状态成功,那么就把返回信息写到指定的层里
|
67 |
if (ajax.readyState == 4 && ajax.status == 200)
|
69 |
msg.innerHTML = ajax.responseText;
|
77 |
<form name= "user_info" method= "post" action= "" >
|
78 |
<input type= "text" name= "user_name" style= "display:none;" />
|
79 |
<input type= "text" name= "user_age" style= "display:none;" />
|
80 |
<input type= "text" name= "user_sex" style= "display:none;" />
|
81 |
<input type= "button" value= "获取服务器变量" onClick= "saveUserInfo()" >
|
ajax_output2.php
http://www.nowamagic.net/ajax/ajax_ExamplesOfPostAndGet.php