环境:vs2019  .net 4.0 framework

根据教材使用scriptmanager在javascript中调用web service 时,失败。现将过程和解决方法记录如下:

1、定义web service

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.web.services;

namespace ajaxtest1
{
    /// <summary>
    /// webservice1 的摘要说明
    /// </summary>
    [webservice(namespace = "http://tempuri.org/")]
    [webservicebinding(conformsto = wsiprofiles.basicprofile1_1)]
    [system.componentmodel.toolboxitem(false)]
    // 若要允许使用 asp.net ajax 从脚本中调用此 web 服务,请取消注释以下行。 
    [system.web.script.services.scriptservice]
    public class webservice1 : system.web.services.webservice
    {

        [webmethod]
        public int gettotal(string s,int x,int y)
        {
            if (s == "+")
            {
                return x + y;
            }
            if (s== "-")
            {
                return x - y;
            }
            if (s == "*")
            {
                return x * y;
            }
            if (s == "/")
            {
                return x / y;
            }
            else
            {
                return 0;
            }
        }
    }
}

2、定义javascript和.aspx页面;

<%@ page language="c#" autoeventwireup="true" codebehind="webform1.aspx.cs" inherits="ajaxtest1.webform1" %>

<!doctype html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>js调用webservice实现运算器</title>
    <script type="text/javascript" >
        function refservice() {
            //alert(document.getelementbyid("text1").value);
            var num1 = document.getelementbyid("text1").value;
            var num2 = document.getelementbyid("text2").value;
            var num3 = document.getelementbyid("select1").value;
            //alert(document.getelementbyid("select1").value);
            webservice1.gettotal(num3, num1, num2, getresult);
            //alert(document.getelementbyid("select1").value);
        }
        function getresult(result) {
            document.getelementbyid("text3").value = result;
            //alert(result);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:scriptmanager id="scriptmanager1" runat="server">
            <services>
                <asp:servicereference  path="~/webservice1.asmx"/>
            </services>
        </asp:scriptmanager>
        请分别输入用于计算的两个整数:<br /><br />
        <input id="text1" type="text" />
        <select id="select1" name="d1">
            <option value="+" selected="selected">+</option>
            <option value="-">-</option>
            <option value="*">*</option>
            <option value="/">/</option>
        </select>
        <input id="text2" type="text" />
        <input id="button1" type="button" value="=" onclick="refservice()" style="height:21px;width:30px"/>
        <input id="text3" type="text" />
        
    </form>
</body>
</html>

整个项目的目录如下:

 

 

3、运行程序,点击“=”,却没有任何效果:

 

4、解决方法:

在脚本中打上断点,发现程序是可以执行到14行的,执行到15行的时候,就执行不下去了

 

 

5、在调用webservice的脚本处,加上命名空间:

 

 运行成功:

 

 

 

总结:可能是教材上的范例年代久远,已经不适用与vs2019了。