Home  >  Article  >  Web Front-end  >  Using JavaScript to call WebServices_javascript tips in VS2008

Using JavaScript to call WebServices_javascript tips in VS2008

WBOY
WBOYOriginal
2016-05-16 16:25:551286browse

The tasks have been completed in the past few days, and there is nothing important. I took the time to learn the knowledge of WebServices. I find it quite interesting and not very difficult.

First, create an asp.net website using VS2008

Secondly, right-click on the project—>Add new item—>Web Service as shown below:

Two files, WebService.cs and WebService.asmx, will be generated
Add code in WebService.cs:

Copy code The code is as follows:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Script.Services;
///
///Summary description of WebService
///

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//Note to add the following code//
[ScriptService]
//To allow this web service to be called from a script using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService : System.Web.Services.WebService
{
Public WebService()
{
//If using designed components, please uncomment the following line
​​​​ //InitializeComponent();
}
[WebMethod]
Public string HelloWorld()
{
         return "Hello World";
}
[WebMethod]
Public int GetSum(int a, int b)
{
         int sum = a b;
        return sum;
}
}

Default.aspx page

Copy code The code is as follows:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml">

   



   
   
   

       
           
               
           

       

       

       
        =
       
       

   

   



通过以上方法就可以轻松的调用WebService中的方法,WebService中也可以返回一个DataSet结果集。
后面还得继续学习WebService的知识。
如果大家有好的WebService学习的资料或者是网站的话,拿出来分享一下,以方便大家共同学习、交流。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn