C# 如何在java脚本函数中调用web服务
我的网络服务C# 如何在java脚本函数中调用web服务,c#,asp.net,web-services,C#,Asp.net,Web Services,我的网络服务 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_Pie
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();
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Services;
使用MySql.Data.MySqlClient;
使用MySql.Data;
使用系统数据;
使用System.IO;
利用制度全球化;
使用系统配置;
使用System.Security.Cryptography;
使用System.Security.Cryptography.X509证书;
使用系统文本;
///
///图表数据的摘要说明
///
[WebService(命名空间=”http://tempuri.org/")]
[WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
//要允许使用ASP.NET AJAX从脚本调用此Web服务,请取消注释以下行。
//[System.Web.Script.Services.ScriptService]
公共类图表\u数据:System.Web.Services.WebService{
MySqlConnection connectionString=新的MySqlConnection(“服务器=***;用户id=***;密码=***;数据库=***;”;
公共图表数据(){
//如果使用设计的组件,请取消注释以下行
//初始化组件();
}
[网络方法]
公共列表绑定_chartvalue()
{
列表图表=新列表();
connectionString.Open();
MySqlCommand=connectionString.CreateCommand();
command.CommandText=“按类从sh_报告组中选择类、计数(类)作为计数”;
MySqlDataReader dr=command.ExecuteReader();
while(dr.Read())
{
图表crt_数据=新图表();
crt_data.Class=dr[0].ToString();
crt_data.COUNT=转换为32(dr[1]);
添加图表(crt_数据);
}
connectionString.Close();
收益表;
}
}
实际上,我是通过web服务检索数据的,但我不知道如何调用该web服务。我像上面那样试过了,但没有得到o/p。它可以简单地显示空页。任何人都知道如何获取web服务数据 您使用的是临时名称空间,它应该是您真正的名称空间,表示“取消对此行的注释以允许从脚本调用”的行仍然没有注释,您的URL将需要指向此服务,而不是页面.aspx。您的服务在哪里,它是如何定义的?您发布的只是您的页面,而您的post函数正在回调该页面,该页面上没有bind\u chartvalue方法。我单独编写服务,而不是在此代码中。您的javascript代码
url:'Ar\u PieChart.aspx/bind\u chartvalue'
需要指向服务定义和公开的url,而不是aspx页面。您的问题中确实没有足够的信息来给出明确的答案。我需要获取web服务数据,并希望将其打印为饼图,我还尝试了web服务URL,但它不起作用。您必须发布完整的示例。您没有将web服务部分包括在解决方案中,因此没有人可以给您答案。请参阅,了解如何改进您的问题。
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;
}
}