A simple example of Ajax WebService call

Source: Internet
Author: User

WebService

<% @ WebService Language = "C #" class = "samples. ASPnet. simplewebservice" %>

Using system;
Using system. Web;
Using system. Web. Services;
Using system. xml;
Using system. Web. Services. Protocols;
Using system. Web. Script. Services;
 
Namespace samples. ASPnet
{

[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
[Scriptservice]
Public class simplewebservice: system. Web. Services. WebService
{

[Webmethod]
Public String echoinput (string input)
{
String inputstring = server. htmlencode (input );
If (! String. isnullorempty (inputstring ))
{
Return string. Format ("You entered {0}."
+ "Current time is {1}.", inputstring, datetime. Now );
}
Else
{
Return "the input string was null or empty .";
}
}

}

}
Sample. aspx

<% @ Page Language = "C #" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">

<Head id = "head1" runat = "server">
<Style type = "text/CSS">
Body {Font: 11pt trebuchet MS;
Font-color: #000000;
Padding-top: 72px;
Text-align: Center}

. Text {Font: 8pt trebuchet MS}
</Style>

<Title> simple Web Service </title>

<SCRIPT type = "text/JavaScript">

// This function callthe web service method.
Function echouserinput ()
{
VaR echoelem = Document. getelementbyid ("enteredvalue ");
Samples. ASPnet. simplewebservice. echoinput (echoelem. value,
Succeededcallback );
}

// This is the callback function that
// Processes the Web service return value.
Function succeededcallback (result)
{
VaR rsltelem = Document. getelementbyid ("Results ");
Rsltelem. innerhtml = result;
}

</SCRIPT>

</Head>

<Body>
<Form ID = "form1" runat = "server">
<Asp: scriptmanager runat = "server" id = "scriptmanager">
<Services>
<Asp: servicereference Path = "simplewebservice. asmx"/>
</Services>
</ASP: scriptmanager>
<Div>
<H2> simple Web Service </H2>
<P> calling a simple service that echoes the user's input and
Returns the current server time. </P>
<Input id = "enteredvalue" type = "text"/>
<Input id = "echobutton" type = "button"
Value = "Echo" onclick = "echouserinput ()"/>
</Div>
</Form>

<HR/>

<Div>
<Span id = "Results"> </span>
</Div>

</Body>

</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.