JQuery Ajax请求具有返回值和输出值的Web方法

时间:2016-11-20 12:32:50

标签: jquery asp.net ajax

我遇到这种奇怪的情况,我在WebService中有一个WebMethod,它需要返回一个通用列表,还需要附加输出参数。

并且$.ajax({ })需要调用此网络服务并在.success()中获取返回值和输出参数。

Web服务如下:

[WebMethod]
public static List < Category > GetAllCategory(out int TotalCountRecord) 
{
     List < Category > AllValues = new List < Category > ();
     //Some ADO.NET Code to Call Stored Procedure with output Value;
     SqlParameter OutParameter = new SqlParameter("@RecordCount", SqlDbType.Int);
     OutParameter.Direction = ParameterDirection.Output;
     cmd.ExecuteNonQuery();
     TotalCountRecord = (int)(OutParameter.Value);
     //Pls. Don't Mind these sequence of the code.
     return AllValues;
}

现在有可能让我为JQuery Ajax调用这些代码

var a = 0;
$.ajax({
   url: 'WebService1.asmx/GetAllCategory',
   method: 'post',
   dataType: 'json',
   data: JSON.stringify({TotalCountRecord: a}),
   contentType: 'application/json; charset=utf-8',
   success: function(data) {

    //Add data to Table
   //Show RecordCount at Footer of the Table
  }

这是解决这种情况的方法吗?

1 个答案:

答案 0 :(得分:1)

我认为这是不可能的。

你可以退出参数:

[WebMethod]
public static dynamic GetAllCategory(out int TotalCountRecord) 
{
 List < Category > AllValues = new List < Category > ();
 //Some ADO.NET Code to Call Stored Procedure with output Value;
 SqlParameter OutParameter = new SqlParameter("@RecordCount",SqlDbType.Int);
 OutParameter.Direction = ParameterDirection.Output;
 cmd.ExecuteNonQuery();
 TotalCountRecord = (int)(OutParameter.Value);
 //Pls. Don't Mind these sequence of the code.
 return new{AllValues,TotalCountRecord};
}

现在在ajax:

success: function(data) {
    Add data.AllValues to Table
    Show data.TotalCountRecord at Footer of the Table
}