WDDX的一个示例,客户端使用JavaScript提交数据,服务器端使用Coldfusion处理数据
<!---载入 wddx.js 文件,此文件位于 coldfusion 服务器 CFIDE/scripts 目录下--->
<script type="text/javascript" src="CFIDE/scripts/wddx.js"></script>
<!---Data binding code--->
<script>
//Generic serialization to a form field
function serializeData(data, formField){
wddxSerializer = new WddxSerializer();
wddxPacket = wddxSerializer.serialize(data);
if(wddxPacket != null){
formField.value = wddxPacket;
}else{
alert("Couldn't serialize data");
}
}
//Person info recordset with columns firstName and lastName
//Make sure the case of field names is preserved
var personInfo = new WddxRecordset(new Array("firstName", "lastName"),true);
//Add next record to end of personInfo recordset
function doNext(){
//Extract data
var firstName = document.personForm.firstName.value;
var lastName = document.personForm.lastName.value;
//Add names to recordset
nRows = personInfo.getRowCount();
personInfo.firstName[nRows] = firstName;
personInfo.lastName[nRows] = lastName;
//Clear input fields
document.personForm.firstName.value = "";
document.personForm.lastName.value = "";
//Show added names on list
//This gets a little tricky because of browser differences
var newName = firstName + "" + lastName;
if(navigator.appVersion.indexOf("MSIE") == -1){
document.personForm.names[length] = new Option(newName, "", false, false);
}else{
//IE version
var entry = document.createElement("OPTION");
entry.text = newName;
document.personForm.names.add(entry);
}
}
</script>
<!---Data collection form--->
<form action="#cgi.SCRIPT_NAME#" method="post" name="personForm">
<!---Input fields--->
Personal information<br>
First name:<input type="text" name="firstName"><br>
Last name: <input type="text" name="lastName"><br>
<br>
<!---Navigation & submission bar--->
<input type="button" value="Next" onClick="doNext()">
<input type="button" value="Serialize" onClick="serializeData(personInfo, document.personForm.wddxPacket)">
<input type="submit" value="Submit">
<br><br>
Name added so far:<br>
<select name="names" size="5"></select>
<br>
<!---This is where the WDDX packet will be stord--->
<!---In a real application this wouls be a hidden input field.--->
<br>
WDDX packet display:<br>
<textarea name="wddxPacket" rows="10" cols="80" wrap="virtual"></textarea>
</form>
<!--- Server-side processing --->
<hr>
<b>Server-side processing</b><br>
<br>
<cfif isdefined("form.wddxPacket")>
<cfif form.wddxPacket neq "">
<!--- Deserialize the WDDX data --->
<cfwddx action="wddx2cfml" input="#form.wddxPacket#" output="personInfo">
<!--- Displaythe query --->
The submitted personal information is:<br>
<cfoutput query="personInfo">
Person #CurrentRow#:#firstName# #lastName#<br>
</cfoutput>
<cfelse>
The client did not send a well-formed WDDX data packet!
</cfif>
<cfelse>
No WDDX data to process at this time.
</cfif>
<!---这个是上面示例的简化版本--->
<!---load the wddx.js file--->
<script type="text/javascript" src="CFIDE/scripts/wddx.js"></script>
<!---Data binding code--->
<script>
//Generic serialization to a form field
function serializeData(data, formField){
wddxSerializer = new WddxSerializer();
wddxPacket = wddxSerializer.serialize(data);
if(wddxPacket != null){
formField.value = wddxPacket;
}else{
alert("Couldn't serialize data");
}
}
</script>
javascript:<br>
<hr><br>
<form action="#cgi.SCRIPT_NAME#" name="form1" method="post">
<input type="text" name="t1">
<input type="hidden" name="h1" />
<input type="button" value="javascript" name="input" onClick="serializeData(t1.value,document.form1.h1)">
<input type="submit" value="submit" />
</form>
coldfusion:<br>
<hr><br>
<cfoutput>
<cfif isdefined("form.h1")>
<cfwddx action="wddx2cfml" input="#form.h1#" output="cf_Value">
</cfif>
<cfif not isdefined('cf_Value')>
<cfset cf_Value = "null">
</cfif>
cf_Value = #cf_Value#
</cfoutput>