Tutorial on helping customers (www.bkjia.com)Asp.net checks whether data can be normally imported into the database. The code is usually written in aspx. in the cs file, if you directly use the server side detection, the page will be refreshed, so that data in some client controls is lost. By submitting the current page through ajax post, you can do this without refreshing the page and detect data.
The example file is as follows:
Ajax. js -- provides the Ajax post method.
Reference content is as follows: Ajax = { Requests: [], ERROR_MESSAGE_URL_NOT_EXISTS: "Url is doesn't exists! ", ERROR_MESSAGE_UNKNOW: "Unknow! ",
_ RequestId: 0, Requests: [],
_ MsXmlId :[ 'Msxml2. XMLHTTP.3.0 ', // 3.0 version is best than other MSXML2.XMLHTTP 'Msxml2. xmlhttp ', 'Microsoft. xmlhttp' ],
_ CreateXhr: function (requestId ){ Var xmlHttp = null; Try { XmlHttp = new XMLHttpRequest (); } Catch (e1 ){ For (var I = 0; I <this. _ msXmlId. length; ++ I ){ Try { XmlHttp = new ActiveXObject (this. _ msXmlId [I]); Break; } Catch (e2 ){
} } }
If (xmlHttp! = Null ){ This. requests [requestId] = {rId: requestId, httpObj: xmlHttp }; This. _ requestId ++; }
Return xmlHttp; }, Post: function (boxId, url, additionalData, handlers ){ Var box = document. getElementById (boxId ); If (! Box |! Url) return;
Var postData = this. _ getData (box );
Var xmlHttp = this. _ createXhr (this. _ requestId ); If (xmlHttp. overrideMimeType ){ XmlHttp. overrideMimeType ("text/html "); }
Var ajax = this; XmlHttp. onReadystatechange = function (){ If (xmlHttp. readyState = 4 ){ If (xmlHttp. status = 200 ){ If (handlers & handlers. success ){ Handlers. success. apply (handlers. success, [xmlHttp]); } } Else { If (handlers & handlers. failure ){ Handlers. failure. apply (handlers. failure, [xmlHttp]); } } } }
XmlHttp. open ("POST", url, true ); // Add this row. sure it is post method // This row must open method belowe XmlHttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded; charset = UTF-8 "); XmlHttp. setRequestHeader ("Connection", "close "); XmlHttp. setRequestHeader ("Content-Length", postData. length ); If (additionalData & additionalData. length> 0 ){ XmlHttp. send (postData + "&" + additionalData ); } Else { XmlHttp. send (postData ); } }, _ GetData: function (box ){ Var fields;
Var daTa = [];
If (box. tagName. toLowerCase () = "form "){ Fields = box. elements; } Else { Fields = this. _ getDataElement (box ); }
For (var I = 0; s = fields. length, I <s; I ++ ){ If (name = fields [I]. name) & (value = fields [I]. value )){ DaTa. push (name + "=" + encodeURIComponent (value )); } }
Return daTa. join ("&"); }, }
|
- Three pages in total:
- Previous Page
- 1
- 2
- 3
- Next Page