Details determine success or failure-Use of Ajax POST method

Source: Internet
Author: User

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.

 

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.