0

这是方法

 [WebMethod]
   public static string[] GetCustCodeFromCustomerName(string[] custName)
   {
       if (custName.Length == 0)
           return null;
       else
       {
           List<string> items = new List<string>();
           StringBuilder custList = new StringBuilder();
           string custListstr = string.Empty;
           for (int i = 0; i < custName.Length; i++)
               custList.Append(custName.ElementAt(i) + ", ");
           custListstr = custList.ToString().Substring(0, custList.Length - 2);
           try
           {
               SqlCommand cmd = new SqlCommand();
               SqlDataReader sdr;
               DataSet ds;
               cmd.CommandText = "sp_sel_custcodeFromCustName";
               cmd.CommandType = CommandType.StoredProcedure;
               cmd.Parameters.AddWithValue("@customercodeinlist", custListstr);
               cmd.Parameters.AddWithValue("@BranchId", Globals.BranchID);
               cmd.Parameters.AddWithValue("@Flag", 1);
               sdr = AppClass.ExecuteReader(cmd);
               ds = AppClass.GetData(cmd);
               while (sdr.Read())
               {
                   items.Add("" + sdr.GetValue(0));
               }
               sdr.Close();
               sdr.Dispose();
               return items.ToArray();
           }
           catch (Exception)
           {
               return null;
           }
       }
   }

我这样称呼它...

custList = $(':checkbox:checked').map(function () { return    $(this).closest('tr').find('.grdCustName').text() }).get();

         $.ajax({
                type: "post",
                url: "~/AutoComplete.asmx/GetCustCodeFromCustomerName",
                data: "{custName:'" + custList + "'}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert(textStatus);
                },
                success: function (result) {
                    alert("success" + result);
                }
            });

我尝试了各种组合,没有contentTypeand datatype,withget而不是 post,但到目前为止似乎仍然没有任何效果。任何人都可以帮忙吗?

4

2 回答 2

1

您必须删除~运算符,我认为Webservice 代码和 json 参数有问题。

看一下示例(.aspx 和 .asmx 都在 webapp 的根目录下):

示例.aspx


<script type="text/javascript">
    $(function () {
        $("#button1").click(function () {
            /* data parameter - array */
            var param = "{'custName': ['aa','bb','cc']}";

            $.ajax({
                type: "POST",
                url: "SampleWs.asmx/GetList",
                data: param,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data) {
                    console.log(data);
                    alert(data.d);
                },
                error: function (xhr, textStatus, errorThrown) {
                    console.log(textStatus + " : " + errorThrown);
                }
            });
        });
    });
</script>

SampleWs.cs


[WebService(Namespace = "http://localhost/jquery/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class SampleWs : System.Web.Services.WebService {
    [WebMethod]
    [ScriptMethod(ResponseFormat=ResponseFormat.Json)]
    public   string[] GetList(string []custName)
    {
        for (int i = 0; i < custName.Length; i++)
            custName[i] = custName[i].ToUpper();
        return custName;  
    }
}
于 2012-08-22T07:45:01.597 回答
0

.get()在此行的末尾您可以调用不带任何参数的调用:

custList = $(':checkbox:checked').map(function () { return    $(this).closest('tr').find('.grdCustName').text() }).get();

您可以考虑删除它。

同样正如其他人所提到的,您应该~从 URL 中删除该字符,因为它仅适用于服务器端代码。

于 2012-08-22T07:53:01.377 回答