Select2js无法使用asp.net webservice方法

时间:2014-10-12 13:55:52

标签: jquery-select2

这是我在asp.net default.aspx页面中使用的客户端脚本:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<link href="select2.css" rel="stylesheet" />
<script src="jquery-1.10.2.js"></script>
<script src="select2.js"></script>
<script type="text/javascript">
     var attendeeUrl = 'WebService.asmx/GetSelect2Data';       
     $(document).ready(function () {
        $("#select2text").select2({
            placeholder: 'Enter names',
            //Does the user have to enter any data before sending the ajax request
            minimumInputLength: 0,            
            allowClear: true,
            multiple: true,
            closeOnSelect: true,
            ajax: {
                //How long the user has to pause their typing before sending the next request
                quietMillis: 150,
                //The url of the json service
                url: attendeeUrl,
                type:'POST',
                dataType: 'json',
                //Our search term and what page we are on
                data: function (term) {
                    return {
                        searchTerm: term
                    };
                },
                results: function (data) {
                    return { results: data};
                }
            }
        });
    });
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<input type="hidden" id="select2text" style="width:200px;" />
</div>
</form>
</body>
</html>

以下是我在网络服务中使用的服务器端代码。

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string GetSelect2Data() {

    IEnumerable<Select2Data> lst = new List<Select2Data>{
    new Select2Data{id="1",text="Smith"},
    new Select2Data{id="2",text="Brown"},
    new Select2Data{id="3",text="Gray"},
    new Select2Data{id="4",text="John"}
    };
    string result = new JavaScriptSerializer().Serialize(lst);
    return result;
}

请帮助我,这将是非常明显的...... 谢谢 Satish kumar

1 个答案:

答案 0 :(得分:0)

请使用以下代码替换您的网络服务方法:

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void GetSelect2Data() {

    IEnumerable<Select2Data> lst = new List<Select2Data>{
    new Select2Data{id="1",text="Smith"},
    new Select2Data{id="2",text="Brown"},
    new Select2Data{id="3",text="Gray"},
    new Select2Data{id="4",text="John"}
    };
    string result = new JavaScriptSerializer().Serialize(lst);       
    this.Context.Response.Write(result);
}

它应该适用于Select2js插件......

由于 萨蒂什南比亚