In ASP. NET, JS simply calls WebServices.

Source: Internet
Author: User

Text address:In ASP. NET, JS simply calls WebServices.

Well, there are two methods for js to call WebService. Well, I know these two methods. If a passing friend knows there are other methods, please give me some advice and make common progress, haha. Well, paste the source code directly.
Method 1:
<% @
Page Language = "C #" 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> test JavaScript WebService call </title>

<SCRIPT type = "text/JavaScript" Language = JavaScript>

Function Test ()

{

// In fact, The helloworld function only has one parameter. The 2nd "Test2" is used to receive the returned value.

// This parameter is not required if no value is returned.

WebService. helloworld ("qinggeer", Test2 );

}
Function Test2 (STR)

{

Alert (STR );
}

</SCRIPT>
</Head>
<Body>
<Form
Id = "form1" runat = "server">
<Asp: scriptmanager
Id = "scriptmanager1"
Runat = "server">

<Services>

<Asp: servicereference Path = "WebService. asmx"
/>

</Services>

</ASP: scriptmanager>
<Input id = "button1"
Type = "button" value = "button" onclick = "javascript: Test ();"
/>

</Form>
</Body>
</Html>
WebService code:

Using
System;
Using system. Web;
Using system. collections;
Using
System. Web. Services;
Using system. Web. Services. Protocols;

///
<Summary>
/// Summary of WebService
///
</Summary>
[WebService (namespace =
Http://tempuri.org/")]
[Webservicebinding (conformsto =
Wsiprofiles. basicprofile1_1)]
[System. Web. Script. Services. 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 Str)
{
Return
STR;
}

[Webmethod]
Public String sayhello (string
Name)
{

Return "hello" + name;
}

}
Method 2:
<% @ Page
Language = "C #" codefile = "test. aspx. cs" inherits = "test" %>

<! 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>
<Title>
Call
WebService with JavaScript and
XMLHTTP.
</Title>
</Head>

<Script
Language = "JavaScript" type = "text/JavaScript">
Function
Requestbyget (URL, data)

{
VaR
XMLHTTP;

Try

{

XMLHTTP = new
Activexobject ("Microsoft. XMLHTTP ");

}

Catch (E)

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.