0

我想Autocomplete为搜索选项创建一个字段。我尝试过使用以下代码。

但是当函数执行时,web 方法不会触发。Autocomplete会是什么原因?

这是jQuery函数:

<script type="text/javascript">
    $(function () { $("#<%=tags.ClientID %>").autocomplete({
         source:function (request, response) {
         $.ajax ({
             type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "~/Services/AutoComplete.asmx/GetFarmersByName",
            data: "{ 'name' : '" + $("#<%=tags.ClientID %>").val() + "'}",
            dataType: "json",
            async: true,
            dataFilter: function (data) { return data; },
            success: function (data) {
                      response($(data.d, function (item) {
                                return            {
                                    value: item.AdrNm
                                }
                       }));
                     },
           error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(textStatus);
                        }
                    });
                }

            });
        });
 </script>

这是网络方法

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<FMISPersonalDataViewByName_Result> GetFarmersByName(string name)
    {
        this._personalData = new personalData();
        int cky = 45;
        CdMa cdMas = new CdMa();
        cdMas = this._personalData.getcdMasByConcdCd2(cky, "AdrPreFix", true);
        int prefixKy = cdMas.CdKy;

        List<FMISPersonalDataViewByName_Result> list = new List<FMISPersonalDataViewByName_Result>();

        list = this._personalData.GetPersonalDataByName(prefixKy, cky, name);
        return list;
    }
4

6 回答 6

3

Make sure you hit the webservice function by having breakpoint on your service function. Please change your script to below:

<script type="text/javascript">
    $(function () {
        $("#<%=tags.ClientID %>").autocomplete
        ({

            source:
            function (request, response) {
                $.ajax
                ({
                    url: " <%=ResolveUrl("~/Services/AutoComplete.asmx/GetFarmersByName") %>",

                    data: "{ 'name' : '" + $("#<%=tags.ClientID %>").val() + "'}",

                    dataType: "json",

                    type: "POST",

                    contentType: "application/json; charset=utf-8",                   

                    async: true,

                    dataFilter: function (data) { return data; },

                    success: function (data) 
                    {
                        response($(data.d, function (item) 
                        {
                            return
                            {
                                value: item.AdrNm
                            }
                        }));
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus);
                    }
                });
            }

        });
    });
</script>
于 2013-07-04T06:37:36.303 回答
2

在您的服务类上方添加 [System.Web.Script.Services.ScriptService]

或者您可以在 asp.net 页面中执行此操作!

添加 static 关键字并将 web 服务更改为 ASP.NET 页面!

 public static string GetFarmersByName(string name)

例如:

一个.aspx:

  $.ajax({
                    type: "POST",
                    url: "A.aspx/GetSN",
                    data: {},
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    async:false,
                    success: function (json) {
                        var msg = JSON.parse(json.d);
                        sn = msg;
                    },
                    failure: function () {
                        alert("Sorry,there is a error!");
                    }
                });

然后在你的 A.aspx.cs 中输入:

 [WebMethod]
        public static string GetSN()
        {
            Random RN = new Random();
            string year = DateTime.Now.ToString("yy").ToString();
            string MonAndDate = DateTime.Now.ToString("MMdd").ToString();
            string Hour = DateTime.Now.ToString("HHMMss").ToString() + DateTime.Now.Millisecond.ToString() + RN.Next(100, 900).ToString();
            string SerialNumber = year + MonAndDate + Hour;
            return JsonConvert.SerializeObject(SerialNumber);
        }
于 2013-07-04T06:56:18.620 回答
1

将此方法粘贴到您调用此方法的文件后面的代码中。将 url 更改为 url: "Test.aspx/GetFarmersByName" 然后进行测试。它的代码非常干净,而不是 Web 服务。

using System.Web.Script.Services;
[WebMethod]
[ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
public List<string> GetFarmersByName(string aaa)
{
    List<string> list = new List<string>();
    list.Add(aaa);
    return list;
}
于 2014-05-13T07:56:23.373 回答
1

假设tags您的文本框,将数据设置为{ 'name': '" + request.term + "'}

$("#<%=tags.ClientID %>").autocomplete({
     source: function (request, response) {
         $.ajax({
             url: "Services/AutoComplete.asmx/GetFarmersByName",
             data: "{ 'name': '" + request.term + "'}",
             dataType: "json",
             type: "POST",
             contentType: "application/json; charset=utf-8",
             success: function (data) {
                 response($.map(data.d, function (item) {
                     return {
                         label: item.split('-')[0],
                         val: item.split('-')[1]
                     }
                 }))
             },
             error: function (response) {
                 alert(response.responseText);
             },
             failure: function (response) {
                 alert(response.responseText);
             },
         });
     },
     minLength: 0,
     focus: function () {
         // prevent value inserted on focus
         return false;
     },
 });

调试 GetFarmersByName 方法,

注意:检查您是否取消[System.Web.Script.Services.ScriptService]了 .asmx 页面上的注释。

于 2013-07-04T07:27:50.090 回答
1

再发帖!!!

测试.aspx:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-1.9.0.min.js"></script>
    <script type="text/javascript">
        $(function(){
            $("#Button1").bind("click",function(){
                $.ajax({
                    type: "POST",
                    url: "Test.asmx/GetFarmersByName",
                    data:"{'aaa':'zhangsan'}",
                    contentType: "application/json;charset=utf-8",
                    dataType: "json",
                    async: false,
                    success: function (json) {

                    },
                    failure: function () {
                        alert("Sorry,there is a error!");
                    }
                });
            })
        })

    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <input id="Button1" type="button" value="button" />
    </div>
    </form>
</body>
</html>

测试.asmx:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;

namespace TestWebForm
{
    /// <summary>
    /// Summary description for Test
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 Test : System.Web.Services.WebService
    {

        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public List<string> GetFarmersByName(string aaa)
        {
            List<string> list = new List<string>();
            list.Add(aaa);
            return list;
        }
    }
}
于 2013-07-04T08:36:44.993 回答
0

尝试这个 -

<script type="text/javascript">
$(function () { $("#<%=tags.ClientID %>").autocomplete({
     source:function (request, response) {
     var obj = JSON.Stringfy("{ 'name' : '" + $("#<%=tags.ClientID %>").val() + "'}");
     $.ajax ({
         type: "POST",
        url: "~/Services/AutoComplete.asmx/GetFarmersByName",
        data: obj,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        async: true,
        dataFilter: function (data) { return data; },
        success: function (data) {
                  response($(data.d, function (item) {
                            return            {
                                value: item.AdrNm
                            }
                   }));
                 },
       error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(textStatus);
                    }
                });
            }

        });
    });

和网络方法-

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<FMISPersonalDataViewByName_Result> GetFarmersByName(string name)
{
    this._personalData = new personalData();
    int cky = 45;
    CdMa cdMas = new CdMa();
    cdMas = this._personalData.getcdMasByConcdCd2(cky, "AdrPreFix", true);
    int prefixKy = cdMas.CdKy;

    List<FMISPersonalDataViewByName_Result> list = new List<FMISPersonalDataViewByName_Result>();

    list = this._personalData.GetPersonalDataByName(prefixKy, cky, name);
    return list;
}
于 2018-03-03T12:03:42.807 回答