JavaScript script calls Web Service method in ASP. Net 4.0

Environment: VS2019. net 4.0 framework

Failed to use ScriptManager to call Web service in JavaScript according to the textbook. Now the process and solution are recorded as follows:

1. Define Web Service

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace AjaxTest1
    /// <summary>
    /// WebService1 Summary description of
    /// </summary>
    [WebService(Namespace = "")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // To allow ASP.NET AJAX Call this from script Web Service, please uncomment the following line. 
    public class WebService1 : System.Web.Services.WebService

        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;
                return 0;

2. Define JavaScript and. aspx pages;

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AjaxTest1.WebForm1" %>

<!DOCTYPE html>

<html xmlns="">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>js call WebService Implement arithmetic unit</title>
    <script type="text/javascript" >
        function RefService() {
            var num1 = document.getElementById("Text1").value;
            var num2 = document.getElementById("Text2").value;
            var num3 = document.getElementById("Select1").value;
            WebService1.GetTotal(num3, num1, num2, GetResult);
        function GetResult(result) {
            document.getElementById("Text3").value = result;
    <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
                <asp:ServiceReference  Path="~/WebService1.asmx"/>
        //Please enter two integers for calculation:<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>
        <input id="Text2" type="text" />
        <input id="Button1" type="button" value="=" onclick="RefService()" style="height:21px;width:30px"/>
        <input id="Text3" type="text" />

The contents of the whole project are as follows:



3. Run the program and click "=" without any effect:


4. Solutions:

When a breakpoint is set in the script, it is found that the program can be executed to 14 lines. When it is executed to 15 lines, it cannot be executed



5. Add the namespace to the script calling WebService:


Run successfully:




Conclusion: it may be that the example in the textbook is too old for VS2019.

Tags: ASP.NET Javascript

Posted on Tue, 17 Dec 2019 12:14:25 -0500 by NoPHPPhD