A simple example of JS WebService call

Source: Internet
Author: User

First, create a web project and directly use the default created ASPX page code.

ASPX page:

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

<! 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 runat = "server">
<Title> </title>

<SCRIPT type = "text/JavaScript">
// The following two methods implement the brushless new handlestatechange of Ajax + WebService. After the callback function is executed, the value returned by helloworld will be assigned to the reuslttext parameter of handlestatechange, which is hard to understand.
Function getstr (){
Jswebservice. helloworld (handlestatechange );
}

function handlestatechange (reuslttext) {
$ get ("result "). innerhtml = reuslttext;
}< br>











& nbsp;




Create a WebService

Jswebservice. CSCodeAs follows:

Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. Web;
Using system. Web. Services;

/// <Summary>
/// Summary of jswebservice
/// </Summary>
[WebService (namespace = "http://tempuri.org/")]
[Webservicebinding (conformsto = wsiprofiles. basicprofile1_1)]
// To allow ASP. Net ajax to call this web service from a script, cancel the comments to the downstream.
[System. Web. Script. Services. scriptservice]
Public class jswebservice: system. Web. Services. WebService {

Public jswebservice (){

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

[Webmethod]
Public String helloworld (){
Return "Hello World ";
}
}

The above is the complete code. Note the following points:

1. The meaning of the two methods in Javascript should be understood, that is, the implementation of refreshing

2. asp: Understand the scriptmanager configuration. The value of the path attribute is the WebService class to be used in Js.

3. The comment of [system. Web. Script. Services. scriptservice] In the WebService class should be canceled. In order to enable js to call this class

 

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.