Ajax (Modify password example)

Source: Internet
Author: User
Today, the study of the day of Permissionbase, which learned the most things, is how to change the page without refreshing password, add users, modify module information, and so on, most of the use of Webhtmlcontrol,javascript, as well as the *. Ashx for the suffix name of the file, the following I will be the simplest to modify the password as an example of the following, to use the three JS is stringhelper.js,xmlhttphelper.js and default.js
First in the default.js, there is a function to click the Modify Password button event,//Modify the Password button functions btnsubmitclicked ()
{
Try
{
if (! _checkinputvalue ()) return;

var updatestr = stringhelper.buildflatxmlstring (
"Staff",
["OldPassword", "NewPassword"],
[Document.all.txtOldPassword.value,
Document.all.txtNewPassword.value]);
var ssucceed = Xmlhttphelper.transmit (False, "post", "text", "vilidateimage.ashx", null, UPDATESTR);
Document.all.Show.value = Ssucceed;
Document.all.txtOldPassword.value = "";
Document.all.txtNewPassword.value = "";
Document.all.txtReNewPassword.value = "";
Document.all.divAlertMess.innerText = "";
}
catch (E)
{
alert (message.clienterror);
Location.reload (TRUE);
}
}
Then the stringhelper.buildflatxmlstring function in the call Stringhelper.jszhon the old password, the new password generates the XML file from the client, stringhelper.buildflatxmlstring = function (Rootname, arrNames, arrvalues)
{
var returnval = ("<" + Rootname + ">");
for (var i = 0; i < arrnames.length i + +)
{
ReturnVal + + ("<" + Arrnames[i] + ">");
if (Arrvalues[i]!= null) ReturnVal = = this. Encodexml (Arrvalues[i]);
ReturnVal + + ("</" + arrnames[i] + ">");
}
ReturnVal + + ("</" + Rootname + ">");
return returnval;
};
returns to Default.js and then calls the. ahsx file with the Xmlhttphelper.transmit function in Xmlhttphelper.js.
//
Use XMLHTTP and remote server communication.
//
Parameter name must be of type value range description
// -------------------------------------------------------------------------------------------
Async is a Boolean true/false whether to use asynchronous methods
HttpMethod is a string "post"/"Get" HTTP method
Responsetype no string "text"/"XML" returns the type of data
The URL is a string that receives the requested URL address
Callback a callback function that executes when an asynchronous operation completes
Data sent when PostData a string post method
//
Note: Non-necessary parameters, such as null instead of being passed.
//
Xmlhttphelper.transmit = function (Async, HttpMethod, Responsetype, URL, callback, PostData)
{
HttpMethod = Httpmethod.tolowercase ();
if (responsetype!= null) Responsetype = Responsetype.tolowercase ();

var xmlhttp = this. __getxmlhttpobj ();
Xmlhttp.open (HttpMethod, url, async);

if (! Async && HttpMethod = "POST")
Xmlhttp.setrequestheader ("Content-type", "application/x-www-form-urlencoded");

if (async)
{
Xmlhttp.onreadystatechange = function ()
{
if (xmlhttp.readystate = = 4 && xmlhttp.status = 200)
{
Try
{
if (typeof (callback) = = "function")
{
Switch (responsetype)
{
Case "Text":
Callback (Xmlhttp.responsetext);
break;
Case "XML":
Callback (Xmlhttp.responsexml);
break;
Default:
callback (NULL);
}
}
}
Finally
{
XMLHTTP = null;
}
}
}
Xmlhttp.send (PostData);
}
Else
{
Xmlhttp.send (PostData);
if (xmlhttp.readystate = = 4 && xmlhttp.status = 200)
{
Switch (responsetype)
{
Case "Text":
return xmlhttp.responsetext;
Case "XML":
return xmlhttp.responsexml;
Default:
return null;
}
}
Else
{
return null;
}
}
}; *.ashx files (mostly background operations)
public void ProcessRequest (HttpContext context)
{
String Ssucceed;
XmlDocument doc = new XmlDocument ();
Doc. Load (context. Request.inputstream);
ssucceed = "Old password:" + Doc. selectSingleNode ("Staff/oldpassword"). InnerText;
Ssucceed + = "New password:" + Doc. selectSingleNode ("Staff/newpassword"). InnerText;
Context. Response.ContentType = "Text/plain";
Context. Response.Write (Ssucceed);
Context. Response.End ();
Finally, return the return value to the Modify Password page to determine whether the modification was successful ...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.