[Original] Using jquery in ASP. net1.1 to call the background WebService File

Source: Internet
Author: User
1. First, add the following statement in <system. Web> in Web. config. Otherwise, the message "the request format cannot be recognized" is displayed"
<WebServices>
<Protocols>
<Add name = "httppost"/>
<Add name = "httpget"/>
</Protocols>
</WebServices>
2. Create a "Web Service" Page, such as the testwebservice. asmx file, and add the following method:

 

C # code

[Webmethod]
Public String checkaccount (string struseraccount)
{
Long lngusercount = 0;
Sqlparameter paramuseraccount = new sqlparameter ("@ useraccount", sqldbtype. varchar, 50 );
Paramuseraccount. value = struseraccount;
Lngusercount = convert. toint64 (sqlhelper. executescalar (dbconnection. sqlserverconnection, commandtype. Text, "select count (*) from user_reg where useraccount = @ useraccount, paramuseraccount ));

If (lngusercount = 0)
Return "OK ";
Else
Return "this user already exists! ";
}

 

3. create a new "Web form", such as testform. aspx: Add a "text box" server control on this page and name it txaccount. Then add the following statement to

<SCRIPT charset = "UTF-8" type = "text/JavaScript" src = "scripts/jquery/jquery-1.4.2.min.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
$ (Document). Ready (function (){
$ ('# Txtaccount'). Blur (function (){
VaR useracc = Document. getelementbyid ('txtaccount'). value;
$. Ajax ({
Type: "Get ",
URL: "WebService/testwebservice. asmx/checkaccount? Struseraccount = "+ useracc,
Data: NULL,
Datatype: "text ",
Success: function (result ){
Alert ("success:" + result );
},
Error: function (result ){
Alert ("error:" + result. responsetext );
}
});
});
});
</SCRIPT>

Or

 

Jquery code

<SCRIPT charset = "UTF-8" type = "text/JavaScript" src = "scripts/jquery/jquery-1.4.2.min.js"> </SCRIPT>
<SCRIPT type = "text/JavaScript">
$ (Document). Ready (function (){
$ ('# Txtaccount'). Blur (function (){
VaR useracc = Document. getelementbyid ('txtaccount'). value;
$. Ajax ({
Type: "Post ",
URL: "WebService/testwebservice. asmx/checkaccount ",
Data: "struseraccount =" + useracc,
Datatype: "text ",
Success: function (result ){
Alert ("success:" + result );
},
Error: function (result ){
Alert ("error:" + result. responsetext );
}
});
});
});
</SCRIPT>

 

4. Run the test

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.