如何在java脚本函数中调用Web服务

本文关键字:调用 Web 服务 函数 脚本 java | 更新日期: 2024-09-24 18:11:37

<%@ 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;
    }
}

实际上,我正在通过Web服务检索数据,但是我不知道如何调用该Web服务。我像上面一样尝试过,但我没有得到 o/p。它只是显示空白页面。任何人都知道如何获取 Web 服务数据。

如何在java脚本函数中调用Web服务

您正在使用临时命名空间,这应该是您的真实命名空间,显示"取消注释此行以允许从脚本调用"的行仍未注释,您的URL需要指向此服务而不是Page.aspx。