马上加入IBC程序猿 各种源码随意下,各种教程随便看! 注册 每日签到 加入编程讨论群

C#教程 ASP.NET教程 C#视频教程程序源码享受不尽 C#技术求助 ASP.NET技术求助

【源码下载】 社群合作 申请版主 程序开发 【远程协助】 每天乐一乐 每日签到 【承接外包项目】 面试-葵花宝典下载

官方一群:

官方二群:

ASP.NET4.0中JavaScript脚本调用Web Service 方法

[复制链接]
查看1677 | 回复2 | 2019-12-26 09:10:19 | 显示全部楼层 |阅读模式

情况:VS2019 .net 4.0 framework

根据课本利用ScriptManager在JavaScript中调用Web service 时,失败。现将过程息争决方法记载如下:

1、界说Web Service

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.Services;
  6. namespace AjaxTest1
  7. {
  8. /// <summary>
  9. /// WebService1 的择要分析
  10. /// </summary>
  11. [WebService(Namespace = "http://tempuri.org/")]
  12. [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
  13. [System.ComponentModel.ToolboxItem(false)]
  14. // 若要答应利用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消解释以下行。
  15. [System.Web.Script.Services.ScriptService]
  16. public class WebService1 : System.Web.Services.WebService
  17. {
  18. [WebMethod]
  19. public int GetTotal(string s,int x,int y)
  20. {
  21. if (s == "+")
  22. {
  23. return x + y;
  24. }
  25. if (s== "-")
  26. {
  27. return x - y;
  28. }
  29. if (s == "*")
  30. {
  31. return x * y;
  32. }
  33. if (s == "/")
  34. {
  35. return x / y;
  36. }
  37. else
  38. {
  39. return 0;
  40. }
  41. }
  42. }
  43. }
复制代码

2、界说JavaScript和.aspx页面;

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AjaxTest1.WebForm1" %>
  2. <!DOCTYPE html>
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
  6. <title>js调用WebService实现运算器</title>
  7. <script type="text/javascript" >
  8. function RefService() {
  9. //alert(document.getElementById("Text1").value);
  10. var num1 = document.getElementById("Text1").value;
  11. var num2 = document.getElementById("Text2").value;
  12. var num3 = document.getElementById("Select1").value;
  13. //alert(document.getElementById("Select1").value);
  14. WebService1.GetTotal(num3, num1, num2, GetResult);
  15. //alert(document.getElementById("Select1").value);
  16. }
  17. function GetResult(result) {
  18. document.getElementById("Text3").value = result;
  19. //alert(result);
  20. }
  21. </script>
  22. </head>
  23. <body>
  24. <form id="form1" runat="server">
  25. <asp:ScriptManager ID="ScriptManager1" runat="server">
  26. <Services>
  27. <asp:ServiceReference Path="~/WebService1.asmx"/>
  28. </Services>
  29. </asp:ScriptManager>
  30. 请分别输入用于盘算的两个整数:<br /><br />
  31. <input id="Text1" type="text" />
  32. <select id="Select1" name="D1">
  33. <option value="+" selected="selected">+</option>
  34. <option value="-">-</option>
  35. <option value="*">*</option>
  36. <option value="/">/</option>
  37. </select>
  38. <input id="Text2" type="text" />
  39. <input id="Button1" type="button" value="=" onclick="RefService()" style="height:21px;width:30px"/>
  40. <input id="Text3" type="text" />
  41. </form>
  42. </body>
  43. </html><br /><br />
复制代码

整个项目标目次如下:

091028pcdaxd111y11bml3.png

3、运行步伐,点击“=”,却没有任何结果:

091028ztsz8yjff9ite9os.png

4、办理方法:

在脚本中打上断点,发现步伐是可以实行到14行的,实行到15行的时间,就实行不下去了

091029tnzx00p702sgl2nk.png

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

091029fmcampxecimipazz.png

运行成功:

091030r9yw7wrfm3y7yaza.png

总结:大概是课本上的范例年代长远,已经不实用与VS2019了。

C#论坛 www.ibcibc.com IBC编程社区
C#
C#论坛
IBC编程社区
*滑块验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则