环境: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 ;
}
}
}
}

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了。

ASP.NET4.0中JavaScript脚本调用Web Service 方法的更多相关文章

  1. asp.net ajax客户端框架如何调用Web Service

    asp.net ajax客户端框架如何调用Web Service 1:Web Service类添加 [System.Web.Script.Services.ScriptService]特性2:需要异步 ...

  2. ASP.Net4.0中新增23项功能

    这篇文章介绍Visual Studio 2010 (ASP.Net 4.0)的新功能. 1.代码片段(Code Snippets): 代码段是预先开发的代码模板,可以节省我们对有关语法思考的时间.在V ...

  3. ASP.NET4.0新特性

    原文:ASP.NET4.0新特性 在以前试用VS2010的时候已经关注到它在Web开发支持上的一些变化了,为此我还专门做了一个ppt,当初是计划在4月12日那天讲的,结果因为莫名其妙的原因导致没有语音 ...

  4. ASP.NET调用Web Service

    1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...

  5. ASP.NET4.0所有网页指令

    ASP.NET网页指令(Page Directive)就是在网页开头的标签声明: <% Page Language="C#" %> 而指令的作用在于指定网页和用户控件编 ...

  6. C#调用接口注意要点 socket,模拟服务器、客户端通信 在ASP.NET Core中构建路由的5种方法

    C#调用接口注意要点   在用C#调用接口的时候,遇到需要通过调用登录接口才能调用其他的接口,因为在其他的接口需要在登录的状态下保存Cookie值才能有权限调用, 所以首先需要通过调用登录接口来保存c ...

  7. 【IHttpHandler】在ASP.Net2.0中使用UrlRewritingNet实现链接重写

    很多时候我们需要链接转向(Url Rewriting),例如二级域名转向.文章访问链接等场合. 让我们看两个例子: 1 你现在看到的当前作者的博客园的域名: http://jx270.cnblogs. ...

  8. <pages validateRequest="false"/>在.net4.0中无效的问题

    再web.config中设置<pages validateRequest="false"/>在.net4.0中无效的问题 解决方案: <system.web> ...

  9. vs2012 aps.net4.0/4.5尚未在web服务器上注册

    安装了vs2015后,vs2012 启动后报错: aps.net4.0/4.5尚未在web服务器上注册 解决办法: 下载微软补丁: http://blogs.msdn.com/b/webdev/arc ...

随机推荐

  1. 【笔记】总结Springboot和Vue前后端分离的跨域问题

    跨域一直是个很玄学的问题,SSM的时候又得前后端一起配置,sb的时候又不用. 前端 axios普通get请求 submitForm() { var v=this; this.$axios({ meth ...

  2. 【NHOI2018】跳伞登山赛

    [题目描述] 某山区有高高低低的 n 个山峰,根据海拔高度的不同,这些山峰由低到高进行了 1 到 n 编号.有 m 条只能单向通行的羊肠小道连接这些山峰.现在,这里要举行一场跳伞登山赛,选手们伞降到某 ...

  3. yum运行报错:File "/usr/libexec/urlgrabber-ext-down", line 28

    [root@sdw1 bin]# vim /usr/libexec/urlgrabber-ext-down 再次执行yum命令,正常下载

  4. 文件和目录权限chmod、更改所有者和所属组chown、umask、隐藏权限lsattr/chattr 使用介绍

    第2周第3次课(3月28日) 课程内容:2.14 文件和目录权限chmod2.15 更改所有者和所属组chown2.16 umask2.17 隐藏权限lsattr/chattr 2.14 文件和目录权 ...

  5. Spring Boot 2.0 学习笔记(一)——JAVA EE简介

    本章内容:JAVA EE>Spring>Spring Boot 一.JAVA EE简介 1.1 Java ee优点:结束了Web开发的技术无序状态,让程序员.架构师用同一种思维去思考如何架 ...

  6. webpack4分包方案

    webpack4放弃了 commonsChunkPlugin,使用更方便灵活智能的 splitChunks 来做分包的操作. 下面有几个例子,并且我们假设所有的chunks大小至少为30kb(采用sp ...

  7. use dict in bash

    declare -A animals=(["moo"]="cow" ["woof"]="dog") for sound ...

  8. Nginx专题(2):Nginx的负载均衡策略及其配置

    本文介绍了Nginx的负载均衡策略,一致性hash分配原理,及常用的故障节点的摘除与恢复配置. 文章来源:宜信技术学院 & 宜信支付结算团队技术分享第一期-宜信支付结算八方数据团队高级技术经理 ...

  9. luogu P2401 不等数列 |动态规划

    题目描述 将1到n任意排列,然后在排列的每两个数之间根据他们的大小关系插入">"和"<".问在所有排列中,有多少个排列恰好有k个"< ...

  10. play-with-docker搭配ffsend完成文件上传及下载(解决从docker hub拉取镜像慢问题)

    由于众所周知的原因,大家有的时候pull docker hub上的镜像是很困难的,下载到99%就这么不动了也是很正常的事情 这个时候以下步骤是100%可以解决问题的: 1.找一台国外的服务器安装doc ...