jQuery select2 masofaviy ma'lumotlar va asp.net bilan

Tanlangan qutilarni almashtirish uchun select2 kutubxonasidan foydalanmoqdaman. Men 7-misolni Select2 kutubxonasi sahifasida topishingiz mumkin bo‘lgan 7-misolni qayta tartibladim (identifikator $("#e7").select2 va hokazo bilan pastga aylantiring... ). Men ketma-ket json ma'lumotlarini qaytaradigan o'zimning umumiy ishlov beruvchimni yaratdim:

GetData.asxh ko'rinishi : umumiy sinf GetData : IHttpHandler { public bool IsReusable { get { return false; } }

    public class RecipesList
    {
        public int total { get; set; }
        public List<TopRecipeTable> recipes { get; set; }

        public RecipesList() { }

        public RecipesList(int total, List<TopRecipeTable> recipes)
        {
            this.total = total;
            this.recipes = recipes;
        }
    }

    private string GenerateJsonSerializedObject(int languageId, string orderBy)
    {            
        RecipesList recipeList = new RecipesList(15, DBDataBase.GetTopRecipesByNumberOfRecipes(languageId, 15));
        return new JavaScriptSerializer().Serialize(recipeList);
    }

    public void ProcessRequest(HttpContext context)
    {
        int languageId;            
        bool languageParsed = int.TryParse(context.Request["languageId"], out languageId);
        string orderBy = (string)context.Request["orderBy"];

        if (languageParsed && orderBy != string.Empty)
        {enter code here
            context.Response.ContentType = "application/json";
            var jsonValue = GenerateJsonSerializedObject(languageId, orderBy);
            context.Response.Write(jsonValue);
        }
    }

Ushbu umumiy ishlov beruvchi jsonning to'g'ri formatini qaytaradi (men uni ushbu URL orqali tekshirdim). Mening natijam (json) ham yuqoridagi sahifadagi misol bilan bir xil. Ammo bundan keyin jQuery endi ishga tushmaydi.

Mening skriptim:

$(document).ready(function () {
        $("#e8").select2({
            placeholder: "Search for a recipe",
            //minimumInputLength: 1,
            ajax: {                               
                url: "/Handlers/GetData.ashx",
                dataType: 'jsonp',
                data: function (term, page) {
                    return {
                        languageId: 1,
                        orderBy: "TA"
                    };
                },
                results: function (data, page) {
                    alert(data.total);
                    var more = (page * 10) < data.total; // whether or not there are more results available

                    // notice we return the value of more so Select2 knows if more results can be loaded
                    return { results: data.recipes, more: more };
                }
            },
            formatResult: movieFormatResult, // omitted for brevity, see the source of this page
            formatSelection: movieFormatSelection, // omitted for brevity, see the source of this page
            dropdownCssClass: "bigdrop", // apply css that makes the dropdown taller
            escapeMarkup: function (m) { return m; } // we do not want to escape markup since we are displaying html in results
        });
    });

Asl misolda xuddi shu alert(data.total) ni yozishga harakat qildim va u ishladi, lekin mening versiyamda emas. Shunday qilib, menda to'g'ri json formati bor, jQuery mening umumiy ishlov beruvchimni chaqiradi, shuningdek, tilId parametrlarini oldi ... va to'g'ri json formatini qaytaradi, lekin hech narsa emas. Men bu erda biror narsa etishmayotganimni bilmayman, chunki bu narsa umumiy ishlov beruvchi bilan ham ishlashi mumkinligiga aminman. Umid qilamanki, men muammom haqida etarli ma'lumot berdim.

I can also add my result in jquery .ajax error handler : 
xhr.status = 200
ajaxOptions = parsererror
horwnError = SyntaxError : invalid label
If this is any helpful information

person janilemy    schedule 12.02.2013    source manba
comment
Siz JavaScript-ni seriyalashtiryapsiz, bu sizga json javobini oladi. Shunga qaramay, ajax qo'ng'irog'ida siz jsonp ma'lumotlar turini ko'rsatdingiz. Uni json ga o'zgartiring va agar sizda boshqa muammolar bo'lmasa, u ishlashi kerak.   -  person DarrellNorton    schedule 11.08.2014


Javoblar (1)


Bu savol ancha eski, shuning uchun sizda hozircha yechim borligiga ishonchingiz komil... lekin:

Ushbu funktsiyalarning barchasini olib tashlang:

formatResult: movieFormatResult formatSelection: movieFormatSelection dropdownCssClass: ... escapeMarkup:....

Siz ma'lumotlaringizni formatlash uchun ushbu funktsiyalarni taqdim etmadingizmi? Bularning barchasi, agar siz maxsus ochiladigan elementlarni yaratsangiz kerak bo'ladi.

Siz data.recipes ni qaytaryapsiz - bu {Matn:"", Id:""} massivi bo'lishi kerak yoki uni o'sha erda qaytarganingizdan yaratishingiz kerak.

Birinchidan, uni juda oddiy ma'lumotlarga ega oddiy ro'yxat bilan ishlang ... keyin u yerdan o'ting.

Bundan tashqari, ushbu ishni bajarganingizda, IHttpHandler o'rniga ma'lumotlaringizni qayta ishlash uchun WebApi yoki ServiceStack-dan foydalaning.

person Wayne Brantley    schedule 04.05.2013