Preliminary Research on Ajax-calling Web Services

Source: Internet
Author: User
Here we will first provide this Web Service:

Using system;
Using system. collections;
Using system. Web;
Using system. Web. Services;
Using system. Web. Services. Protocols;
Using system. Web. Script. Services;
/// <Summary>
/// Summary of WebService
/// </Summary>
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
[Scriptservice]
Public class WebService: system. Web. Services. WebService {

Public WebService (){

// If you use the designed component, uncomment the following line
// Initializecomponent ();
}

[Webmethod]
Public String helloworld (string instr ){
String STR = "Server ASP. Net Ajax gets the information you entered: "+ instr +" <br/> your IP address is :";
STR + = system. Web. httpcontext. Current. Request. userhostaddress;
STR + = "<br/> current time :";
STR + = system. datetime. Now. tolocaltime ();
Return STR;
}

}

The following is the source file:

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.1 // en" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "head1" runat = "server">
<Title> Hello </title>
<SCRIPT type = "text/JavaScript" Language = "JavaScript">
// Call the callback function after the Web Service is successful
Function onshow (result) // The parameter here is the value returned after the Web function is called.
{
VaR S = $ get ("MSG"); // you can also use document. getelementbyid ("MSG") to obtain the HTML control "MSG ";

S. innerhtml = result. tostring ();
}
Function sayhello ()
{
VaR FS = WebService; // the WebService here is a class in the Web Service (do not reverse it with the file name)
FS. helloworld (document. getelementbyid ("testmsg"). Value, onshow); // onshow is the function called after the Web function is called successfully and the returned value is passed to it.
Return false;
}
</SCRIPT>
<Style type = "text/CSS">
# Text1
{
Width: 206px;
}
</Style>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Asp: scriptmanager id = "scriptmanager1" runat = "server">
<Services>
<Asp: servicereference Path = "WebService. asmx"/> // call a Web Service
</Services>
</ASP: scriptmanager>
<Div style = "width: 400px; margin-top: 50px; text-align: Center">
<Input id = "testmsg" type = "text" value = "Hello Ajax"/> <br/>
<Asp: button id = "button1" runat = "server" onclientclick = "Return sayhello ()"
TEXT = "Submit to Web Service"/>
<Br/>
</Div>
<Div id = "MSG"> the information is displayed here ..... </Div>
</Form>
</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.