为什么这个webmethod不被$ .ajax调用?

时间:2012-08-22 07:40:59

标签: jquery ajax xmlhttprequest

这是方法

 [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);
                }
            });

我尝试了各种组合,没有contentTypedatatype,而get而不是帖子,但到目前为止仍然没有效果。有人可以帮忙吗?

2 个答案:

答案 0 :(得分:1)

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

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

<强> sample.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;  
    }
}

答案 1 :(得分:0)

您可以拨打.get(),在此行的末尾没有参数:

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

您可以考虑将其删除。

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