Jquery entry (9): Ajax (call the page method and WebService)

Source: Internet
Author: User

Call the PAGE method andWebService

Note:

In web. config, you must specify that the Web service can be called.

It must be called in post mode.

The Web service returns an XML string, so the client needs to convert it to JSON for further processing.

[Webmethod]

Public static StringAjaxservicetest (StringStr)

{

Return String. Format ("Hello, {0 }", STR );

[System. Web. Script. Services.Scriptservice]

Public Class Testwebservice: System. Web. Services.WebService

{

[Webmethod]

Public StringHelloworld (StringS)

{

Return "Hello World"+ S;

}

FunctionWebservicemethod (){

$. Ajax ({

Type:"Post",

Contenttype:"Application/JSON; UTF-8",

URL:"Testwebservice. asmx/helloworld",

Data:"{S :"""+ $ ("# Inputname"). Val () +"""}",// Pass the WebService method parameters. S is the method parameter and must be consistent.

Success:Function(Data ){

// Convert the returned string to a JSON object

VaRJSON =Null;

Try{

JSON = eval ('('+ Data +')');

}Catch(E ){

Alert ("The returned string is not in JSON format! ");

Return;

}

Alert (JSON. D );

}

});

}

FunctionPageajaxservicetest (){

$. Ajax ({

Type:"Post",

Contenttype:"Application/JSON; UTF-8",

URL:"Pageservice. aspx/ajaxservicetest",

Data:"{STR :"""+ $ ("# Inputname"). Val () +"""}",// Parameters passed to. CS static method. STR is a method parameter and must be consistent

Success:Function(Data ){

// Convert the returned string to a JSON object

VaRJSON =Null;

Try{

JSON = eval ('('+ Data +')');

}Catch(E ){

Alert ("The returned string is not in JSON format! ");

Return;

}

Alert (JSON. D );

}

});

}

</Script>

<Div>

<Input ID= "Inputname" Type= "Text" Value= "" />

A href =" # " onclick =" webservicemethod (); "> call Web service A > Br /> Br />

A href =" # " onclick =" pageajaxservicetest (); "> call Static Page methods A > Br /> Br />

</Div> 

Note:

$. Post ("Pagingservice. asmx/getloginhtml", {S:"FDA"},

Function(Data ){

$ ("# Ajaxlogin"Pai.html (data );

Alert (data );

},"Html");

 

Related Article

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.