Some simple basic knowledge and some small details may determine the success or failure of a function or even a software.
A small function was provided a few days ago. When a user registers, Ajax is used to check whether the user name has been registered. In this small project, we only use Ajax here, so we do not plan to use prototype, which is commonly used in the past. At the beginning, I used the get method to submit data. Later I thought it would be better to change it to post. There is no other reason, because prototype uses the POST method by default, So I think post looks a bit pleasing to the eye. Written JavascriptCodeAs follows:
Javascript
VaR Ajax = Function (Option ){
VaR Request;
VaR Createrquest = Function (){
VaR Httprequest;
Try {
Httprequest = New Activexobject ( " Msxml2.xmlhttp " );
} Catch (E ){
Try {
Httprequest = New Activexobject ( " Microsoft. XMLHTTP " );
} Catch (E1 ){
Httprequest = New XMLHttpRequest ();
}
}
Return Httprequest;
}
VaR Sendrequest = Function (){
Request = Createrquest ();
Request. Open ( " Post " , Option. url, True );
Request. onreadystatechange = Responserequest;
Request. Send (option. Param );
}
VaR Responserequest = Function (){
If (Request. readystate = 4 ){
If (Request. Status = 200 ){
Option. Success (request );
}
Else {
Option. Failure (request );
}
}
}
Sendrequest ();
}
VaR Onblur = Function (EL)
{
VaR Option = {
URL: " Handler. ashx " ,
Param: " TEXT = ABV " ,
Success: Function (Request ){
Alert (request. responsetext );
},
Failure: Function (Request ){
Alert ( False );
}
};
New Ajax (option );
}
The background code is as follows:
Handler
Public Sub Processrequest ( Byval Context As Httpcontext) Implements Ihttphandler. processrequest
Context. response. contenttype = " Text/plain "
Context. response. Write (context. Request. querystring. Count)
Dim Text As String = Context. Request. Form ( " Text " )
Context. response. Write (text)
End sub
During the test, we found that the backend could not obtain the data from the front-end. After reading the book, I only said that the get method was not the POST method. Then I went crazy about searching for information on Baidu. It was really hard work. I finally found it. I had to add a sentence after the open method:
VaR sendrequest = Function (){
Request = Createrquest ();
Request. Open ( " Post " , Option . Url, True );
Request. setRequestHeader ( " Content-Type " , " Application/X-WWW-form-urlencoded " );
Request. onreadystatechange = Responserequest;
Request. Send ( Option . Param );
}
OK, it's easy to get things done. It's really a waste of effort to break the tie shoes.
I have not specifically studied why I should add this sentence:
Request. setRequestHeader ("Content-Type","Application/X-WWW-form-urlencoded");
I hope you will not be enlightened.