如何使用jquery在asp.net母版页中调用Web服务方法 [英] how to call a web service method in asp.net master page using jquery

查看:71
本文介绍了如何使用jquery在asp.net母版页中调用Web服务方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的服务





  public 列表< chartentity> chartAnalysis( string  data1, string  data2, string  loc)
{
string str1 = data1;
string str2 = data2;
string location = loc;
列表< chartentity> chartinfo = new 列表< chartentity>();
DataSet ds4 = new DataSet();
使用(SqlConnection con = new SqlConnection( 数据源= xxxx;用户ID = sxxxa;密码= xxx;数据库= xxx))
{
使用(SqlCommand cmd = new SqlCommand())
{
// cmd.CommandText =exec sp_AreaWiseSales_DashBoard_REP'2012-02-02','2012-07-08',1;
cmd.CommandText = exec sp_AreaWiseSales_DashBoard_REP' + str1 + ',' + str2 + ',' + location + ';
cmd.Connection = con;
使用(SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(ds4);
}
}
con.Close();
}
如果(ds4!= null
{
if (ds4.Tables.Count& gt; 0
{
if (ds4.Tables [ 0 ]。Rows.Count& gt; 0
{
foreach (DataRow dr in ds4.Tables [ 0 ]。行)
{
chartinfo.Add( new ChartEntity {Name = dr [ AreaName]。ToString(),< span class =code-sdkkeyword> value = Convert.ToInt32(dr [ Amt])});
}
}
}
}

return chartinfo;
}



这是我的asp.net代码

 $。ajax ({
类型:POST,
contentType:application / json; charset = utf-8,
url:Services / New.asmx / chartAnalysis,//我是在这里调用服务。但它不是调用方法我怎么能调用它。有另一种方法来调用服务方法


数据:JSON.stringify({data1: 2012-02-05,data2:2012-05-05,loc:1}),

totaldata:{},{},
dataType: json,
success:function(Result){
Result = Result.d;
var data = [];

for(var i in Result){
var serie = new Array(Result [i] .Name,Result [i] .value);
data.push(系列);
}
DreawChart3(数据);
},
});





如何解决这个问题

解决方案

.ajax({
type:POST,
contentType:application / json; charset = utf-8,
url:Services / New.asmx / chartAnalysis,//我在这里调用服务。但是我没有调用该方法如何调用它。还有另一种调用服务方法的方法


data:JSON.stringify({data1:2012-02-05,data2:2012-05-05,loc:1}),

totaldata:{}, {},
dataType:json,
success:function(Result){
Result = Result.d;
var data = [];

for(结果中的var i){
var serie = new Array(Result [i] .Name,Result [i] .value);
data.push(serie);
}
DreawChart3(数据);
},
});





如何解决此问题


您需要将方法标记为WebMethod。示例可以在此处 [ ^ ]



如果您仍有问题,请使用浏览器的开发工具(f12)或Fiddler之类的东西来检查网络流量,以显示任何错误消息等。

This is my service


public List<chartentity> chartAnalysis(string data1, string data2, string loc)
       {
           string str1 = data1;
           string str2 = data2;
           string location = loc;
           List<chartentity> chartinfo = new List<chartentity>();
           DataSet ds4 = new DataSet();
           using (SqlConnection con = new SqlConnection("Data Source=xxxx;User Id=sxxxa;Password=xxx;DataBase=xxx"))
           {
               using (SqlCommand cmd = new SqlCommand())
               {
                   //cmd.CommandText = "exec sp_AreaWiseSales_DashBoard_REP '2012-02-02','2012-07-08',1";
                   cmd.CommandText = " exec sp_AreaWiseSales_DashBoard_REP '" + str1 + "' ,'" + str2 + "','" + location + "'";
                   cmd.Connection = con;
                   using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                   {
                       da.Fill(ds4);
                   }
               }
               con.Close();
           }
           if (ds4 != null)
           {
               if (ds4.Tables.Count &gt; 0)
               {
                   if (ds4.Tables[0].Rows.Count &gt; 0)
                   {
                       foreach (DataRow dr in ds4.Tables[0].Rows)
                       {
                           chartinfo.Add(new ChartEntity { Name = dr["AreaName"].ToString(), value = Convert.ToInt32(dr["Amt"]) });
                       }
                   }
               }
           }

           return chartinfo;
       }


This is my asp.net code

$.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    url: "Services/New.asmx/chartAnalysis",  //I am calling the service here.But it is not call to the method how can i call it.There is another way to call the service method 
                    
                    
                    data: JSON.stringify({ data1: "2012-02-05", data2: "2012-05-05", loc: "1" }),
                    
                    totaldata: "{},{}",
                    dataType: "json",
                    success: function (Result) {
                        Result = Result.d;
                        var data = [];

                        for (var i in Result) {
                            var serie = new Array(Result[i].Name, Result[i].value);
                            data.push(serie);
                        }
                        DreawChart3(data);
                    },
});



How can i solve this problem

解决方案

.ajax({ type: "POST", contentType: "application/json; charset=utf-8", url: "Services/New.asmx/chartAnalysis", //I am calling the service here.But it is not call to the method how can i call it.There is another way to call the service method data: JSON.stringify({ data1: "2012-02-05", data2: "2012-05-05", loc: "1" }), totaldata: "{},{}", dataType: "json", success: function (Result) { Result = Result.d; var data = []; for (var i in Result) { var serie = new Array(Result[i].Name, Result[i].value); data.push(serie); } DreawChart3(data); }, });



How can i solve this problem


You need to mark your method as being a WebMethod. Examples can be found here[^]

If you still have problems then use the browser's dev tools (f12) or something like Fiddler to examine the network traffic to reveal any error messages etc.


这篇关于如何使用jquery在asp.net母版页中调用Web服务方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆