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)
|