Bootstrap Select2 ProcessResults 未触发

Bootstrap Select2 ProcessResults Not Firing

我在使用 Select2 v4 (https://select2.github.io/) 填充 json 结果选项时遇到问题。我一定是忽略了一些小东西,就是不能指手画脚...

下拉菜单:

    <select id="e1" multiple="multiple"></select>

JavaScript:

    <script type="text/javascript">
        $("#e1").select2({
            minimumInputLength: 1,  
            ajax: {
                url: "/Empl.asmx/AccessRemoteData",  
                dataType: 'json',
                delay: 250,
                data: function (params) {
                    return {
                        q: params.term
                    };
                },
                // Why is this never called???
                processResults: function (data) {
                    return { results: data };
                }
            },
            minimumInputLength: 3
        });
    </script>

隐藏代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using Newtonsoft.Json;

namespace WebUI
{
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Empl : System.Web.Services.WebService
    {
        public class Select2DTO
        {
            public string id { get; set; }
            public string text { get; set; }
        }

        [WebMethod]
        public string AccessRemoteData(string q)
        {
            Dictionary<string, string> dictCountry = new Dictionary<string, string>();
            dictCountry.Add("1", "India");
            dictCountry.Add("2", "Canada");
            dictCountry.Add("3", "United States");
            dictCountry.Add("4", "United Kingdom");
            dictCountry.Add("5", "United Arab Emirates");
            List<object> CountryList = new List<object>();
            Select2DTO singleCountry;

            var selectedCountryList = dictCountry.Where(m => m.Value.ToLower().Contains(q)).ToList();
            foreach (var selectedCountry in selectedCountryList)
            {
                singleCountry = new Select2DTO();
                singleCountry.id = selectedCountry.Key;
                singleCountry.text = selectedCountry.Value;
                CountryList.Add(singleCountry);
            }

            var json = JsonConvert.SerializeObject(CountryList, new JsonSerializerSettings() { Formatting = Newtonsoft.Json.Formatting.None });
            return json;
        }
    }
}

正在调用网络服务。如果我转到 http://localhost:52657/Empl.asmx/AccessRemoteData?q=united,我会得到以下结果:

This XML file does not appear to have any style information associated with it. The document tree is shown below.
<string xmlns="http://tempuri.org/">
[{"id":"3","text":"United States"},{"id":"4","text":"United Kingdom"},{"id":"5","text":"United Arab Emirates"}]
</string>

如果我在客户端的 "processResults" 中放置一个断点,它永远不会被击中。我今天早上看了其他几篇文章,但似乎找不到问题所在。

有什么帮助吗?

实际上,整个问题的存在是因为您试图超越 ASP.Net 网络服务。您只需要 return List<Select2DTO> 到客户端。 this.

一篇很好的文章解释了这一切(一个非常常见的陷阱)
    [WebMethod]
    public List<object> AccessRemoteData(string q)
    {
        Dictionary<string, string> dictCountry = new Dictionary<string, string>();
        dictCountry.Add("1", "India");
        dictCountry.Add("2", "Canada");
        dictCountry.Add("3", "United States");
        dictCountry.Add("4", "United Kingdom");
        dictCountry.Add("5", "United Arab Emirates");
        List<object> CountryList = new List<object>();
        Select2DTO singleCountry;

        var selectedCountryList = dictCountry.Where(m => m.Value.ToLower().Contains(q)).ToList();
        foreach (var selectedCountry in selectedCountryList)
        {
            singleCountry = new Select2DTO();
            singleCountry.id = selectedCountry.Key;
            singleCountry.text = selectedCountry.Value;
            CountryList.Add(singleCountry);
        }

        return CountryList;
    }