如何在javascript函数中调用web服务

时间:2015-03-03 14:43:26

标签: c# asp.net web-services

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Ar_PieChart.aspx.cs" Inherits="Ar_PieChart" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <meta http-equiv='content-type' content='text/html; charset=UTF-16' />
    <meta name="viewport" content="width=device-height,minimum-scale=0.5,maximum-scale=3.0,user-scalable=yes" />
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="//www.google.com/jsapi"></script>
    <script type="text/javascript">
        google.load('visualization', '1', { packages: ['corechart'] });
    </script>
    <script type="text/javascript">
        $(document).ready(function () {
            $.ajax({
                type: 'POST',
                dataType: 'json',
                contentType: 'application/json',
                url: 'Ar_PieChart.aspx/bind_chartvalue',
                data: '{}',
                success:
                    function (response) {
                        drawVisualization(response.d);
                    }
            });
        })
        function drawVisualization(piedata) {
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'CLass');
            data.addColumn('int', 'count');
            for (var i = 0; i < piedata.length; i++) {
                data.addRow([piedata[i].CLass, piedata[i].count]);
            }
            new google.visualization.PieChart(document.getElementById('visualization')).draw(data, { is3D: true });
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div id="visualization" style="width: 600px; height: 350px">

    </div>
    </form>
</body>
</html>

背后的代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Services;
using MySql.Data.MySqlClient;

public partial class Ar_PieChart : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        piedata.chart_data service = new piedata.chart_data();
        service.bind_chartvalue();

    }
}

我的网络服务

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using MySql.Data.MySqlClient;
using MySql.Data;
using System.Data;
using System.IO;
using System.Globalization;
using System.Configuration;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;

/// <summary>
/// Summary description for chart_data
/// </summary>
[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 chart_data : System.Web.Services.WebService {

    MySqlConnection connectionString = new MySqlConnection("server=****;user id=****;Password=*****;database=****;");

    public chart_data () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public List<chart> bind_chartvalue()
    {
        List<chart> chart = new List<chart>();

        connectionString.Open();
        MySqlCommand command = connectionString.CreateCommand();
        command.CommandText = "Select Class,COUNT(Class) AS COUNT from sh_report GROUP BY Class";
        MySqlDataReader dr = command.ExecuteReader();
        while (dr.Read())
        {
            chart crt_data = new chart();
            crt_data.Class = dr[0].ToString();
            crt_data.COUNT = Convert.ToInt32(dr[1]);
            chart.Add(crt_data);
        }
        connectionString.Close();
        return chart;
    }
}

实际上我是通过网络服务检索数据,但我不知道如何调用该网络服务。我试过上面的但是我没有得到o / p。它简单显示空白页面。任何人都知道如何获取Web服务数据。

1 个答案:

答案 0 :(得分:0)

您正在使用临时命名空间,它应该是您真正的命名空间,“取消注释此行以允许从脚本调用”的行仍然是未注释的,您的URL将需要指向此服务而不是页面。 ASPX。