ASP.NET4.0中JavaScript脚本调用Web Service 方法
环境: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 方法的更多相关文章
- asp.net ajax客户端框架如何调用Web Service
asp.net ajax客户端框架如何调用Web Service 1:Web Service类添加 [System.Web.Script.Services.ScriptService]特性2:需要异步 ...
- ASP.Net4.0中新增23项功能
这篇文章介绍Visual Studio 2010 (ASP.Net 4.0)的新功能. 1.代码片段(Code Snippets): 代码段是预先开发的代码模板,可以节省我们对有关语法思考的时间.在V ...
- ASP.NET4.0新特性
原文:ASP.NET4.0新特性 在以前试用VS2010的时候已经关注到它在Web开发支持上的一些变化了,为此我还专门做了一个ppt,当初是计划在4月12日那天讲的,结果因为莫名其妙的原因导致没有语音 ...
- ASP.NET调用Web Service
1.1.Web Service基本概念 Web Service也叫XML Web Service WebService是一种可以接收从Internet或者Intranet上的其它系统中传递过来的请求, ...
- ASP.NET4.0所有网页指令
ASP.NET网页指令(Page Directive)就是在网页开头的标签声明: <% Page Language="C#" %> 而指令的作用在于指定网页和用户控件编 ...
- C#调用接口注意要点 socket,模拟服务器、客户端通信 在ASP.NET Core中构建路由的5种方法
C#调用接口注意要点 在用C#调用接口的时候,遇到需要通过调用登录接口才能调用其他的接口,因为在其他的接口需要在登录的状态下保存Cookie值才能有权限调用, 所以首先需要通过调用登录接口来保存c ...
- 【IHttpHandler】在ASP.Net2.0中使用UrlRewritingNet实现链接重写
很多时候我们需要链接转向(Url Rewriting),例如二级域名转向.文章访问链接等场合. 让我们看两个例子: 1 你现在看到的当前作者的博客园的域名: http://jx270.cnblogs. ...
- <pages validateRequest="false"/>在.net4.0中无效的问题
再web.config中设置<pages validateRequest="false"/>在.net4.0中无效的问题 解决方案: <system.web> ...
- vs2012 aps.net4.0/4.5尚未在web服务器上注册
安装了vs2015后,vs2012 启动后报错: aps.net4.0/4.5尚未在web服务器上注册 解决办法: 下载微软补丁: http://blogs.msdn.com/b/webdev/arc ...
随机推荐
- 2019-9-20:渗透测试,基础学习,phpstudy搭建Wordpress,Burpsuite抓取WorePress cms的post包
一.搭建WordPress的cms网站管理系统 1,下载Wordpress cms源码,下载地址:https://wordpress.org/download/ 2,将源码解压到phpstudy目录下 ...
- springboot+swagger接口文档企业实践(上)
目录 1.引言 2.swagger简介 2.1 swagger 介绍 2.2 springfox.swagger与springboot 3. 使用springboot+swagger构建接口文档 3. ...
- 【Luogu P1714】切蛋糕(面向对象编程首次尝试?)
Luogu P1714 题目的大意就是给定一个长度为n的序列,求出这个序列中长度不超过m的子串的最大和 很容易想出的一个解法就是枚举起点终点,直接暴力扫一遍得出答案. 当然也很容易发现这种做法肯定会T ...
- 计蒜客 The Preliminary Contest for ICPC Asia Nanjing 2019
F Greedy Sequence You're given a permutation aa of length nn (1 \le n \le 10^51≤n≤105). For each ...
- 数据表与简单java类映射转换
简单的Java类的定义来源于数据表的结构, 例如:雇员信息表.部门信息表描述的就是雇员或部门的信息, 在实际的开发之中,数据表和简单java类之间的映射关系如下: 1. 数据实体表设计 = 类的定义: ...
- 设置颜色的函数:rgb,hsv,color palettes
1.grb函数 (1)功能: 通过对给定的三个基本色红,绿,蓝的颜色饱和度(intensity)的设定,而创造颜色. 阿尔法透明度(alpha transparent):其值也能被指定,从0到max ...
- 使用Git上传文件到github
第一次利用git连接github时往往都不会勾选Initialize this repository with a README,这样的的确确是简单了,但是如果我们需要勾选,勾选了之后应该怎么办呢?1 ...
- 在Spring Boot中添加全局异常捕捉提示
在一个项目中的异常我们我们都会统一进行处理的,那么如何进行统一进行处理呢? 全局异常捕捉: 新建一个类GlobalDefaultExceptionHandler, 在class注解上@Controll ...
- 【SSL1194】最优乘车
题面: 正文: 把每个边用链式前向星存起来,边权为\(1\),就可以愉♂快♂地最短路了
- 第五章 Unity中的基础光照(1)
[TOC] 渲染总是围绕着一个基础问题:我们如何决定一个像素的颜色?从宏观上来说,渲染包括了两大部分:决定一个像素的可见性,决定这个像素上的光照计算.而光照模型用于决定在一个像素上进行怎样的光照计算. ...