从asp.net中的Javascript调用ASMX web服务

从asp.net中的Javascript调用ASMX web服务,javascript,c#,asp.net,web-services,Javascript,C#,Asp.net,Web Services,我已经构建了一个ASMXWeb服务,它从本地sql数据库中读取数据,我想连接到我的web应用程序。我已经添加了一个服务引用,唯一的问题是我不知道如何在我的web应用程序中直接调用web服务 以下是我的asmx web服务的一部分: <%@ WebService Language="C#" Class="PostWebService" %> using System; using System.Web; using System.Web.Services; using System.

我已经构建了一个ASMXWeb服务,它从本地sql数据库中读取数据,我想连接到我的web应用程序。我已经添加了一个服务引用,唯一的问题是我不知道如何在我的web应用程序中直接调用web服务

以下是我的asmx web服务的一部分:

<%@ WebService Language="C#" Class="PostWebService" %>

using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
[System.Web.Script.Services.ScriptService]

public class PostWebService  : System.Web.Services.WebService {

    //method to get heart rates from sql database
    [WebMethod]
    public List<int> GetHeartRates() {
        var heartRate = new List<int>();
        try
        {
            using (SqlConnection connection = new SqlConnection(@"Data Source = (local); Initial Catalog =  RatesDB; Integrated Security=True"))
            using (SqlCommand cmd = new SqlCommand(" select HeartRate from Rates", connection))
            {
                connection.Open();
                var rdr = cmd.ExecuteReader();

                while (rdr.Read())
                {
                    heartRate.Add((int)rdr[0]);
                }
                rdr.Close();
                connection.Close();
            }
        }
        catch
        {
        }
        return heartRate;
    }

使用制度;
使用System.Web;
使用System.Web.Services;
使用System.Web.Services.Protocols;
使用系统数据;
使用System.Data.SqlClient;
使用系统配置;
使用System.Collections.Generic;
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
[System.Web.Script.Services.ScriptService]
公共类PostWebService:System.Web.Services.WebService{
//方法从sql数据库获取心率
[网络方法]
公共列表GetHeartRates(){
var heartRate=新列表();
尝试
{
使用(SqlConnection连接=新的SqlConnection(@“数据源=(本地);初始目录=速率数据库;集成安全性=真”))
使用(SqlCommand cmd=newsqlcommand(“从速率中选择心率”,连接))
{
connection.Open();
var rdr=cmd.ExecuteReader();
while(rdr.Read())
{
心率.Add((int)rdr[0]);
}
rdr.Close();
connection.Close();
}
}
抓住
{
}
返回心率;
}
这是我的.aspx网页的一部分

    <html>
  <head>
      <script src="https://d3js.org/d3.v4.min.js"></script>
   <script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   <script type="text/javascript">
            google.charts.load('current', {
        'packages': ['gauge']
      });

       d3.csv("brT16.csv", function (rrdata) {
           d3.csv("hrT16.csv", function (hrdata) {
                   for (var i = 0; i < rrdata.length; i++) {
                       for (var j = 0; j < hrdata.length; j++) {
                               console.log(rrdata[i]);
                               console.log(hrdata[j]);
                       }
               }

                   count = 0;
                   count2 = 0;
                   google.charts.setOnLoadCallback(drawChart);
                   function drawChart() {

                       // respiration gauge
                       var data = google.visualization.arrayToDataTable([
                           ['Label', 'Value'],
                           ['RR', 0],
                           //['Heart Rate', 0],
                       ]);

                       var options = {
                           width: 600,
                           height: 250,
                           redFrom: 35,
                           redTo: 55,
                           yellowFrom: 25,
                           yellowTo: 35,
                           minorTicks: 5,
                           max: 50,
                       };

                       var chart = new google.visualization.Gauge(document.getElementById('chart_div'));

                       chart.draw(data, options);

                       setInterval(function () {
                           data.setValue(0, 1, parseInt(rrdata[count][0]));
                           chart.draw(data, options);
                           count++;
                       }, 1000);

google.charts.load('current'{
“包装”:[“规格”]
});
d3.csv(“brT16.csv”,函数(rrdata){
d3.csv(“hrT16.csv”,函数(hrdata){
对于(var i=0;i

我想直接调用web服务,这样它就可以从sql数据库而不是csv中读取内容。如果有任何正确的提示,我将不胜感激。

在向web服务添加引用时,请确保您正在单击“高级”按钮并添加web引用(高级->添加web引用->粘贴Url->添加引用)。之后您应该能够调用web服务方法。

您愿意使用jQuery吗?您可以看看这篇文章。我看了这篇文章,但没有看到如何在图中调用web服务以便它从中读取值,除非我完全忽略了itI。我添加了web引用,但我不知道cal的确切语法如果您是从JavaScript调用web服务,请尝试JQuery.Ajax()确保数据类型匹配。否则,在使用名称空间添加时,您应该能够在后端代码中直接调用该方法。我尝试添加jquery.ajax方法,但无论何时添加并运行网页,图表都不再可见。此外,如果我成功调用web服务,我将如何调用我想要的特定方法那个web服务?在Ajax方法中调用类似url的东西:“someUrl.com/GetHeartRate”。如果您有参数使用数据传递它:“{'TransactionID':'“+TransactionID+”}”我如何调用Ajax()方法来调用web服务而不会弄乱图形?我将它放在代码中,但它使图形不再可见。