select2与ajax post方法

前端之家收集整理的这篇文章主要介绍了select2与ajax post方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在尝试使用 select2与ajax加载.

这是我的代码

clonedTemplate.find('[id^=detailsPhaseFinanceMinor_]').select2({
    placeholder: "Select",minimumInputLength: 1,ajax: { // instead of writing the function to execute the request we use Select2's convenient helper
        type: 'POST',contentType: "application/json; charset=utf-8",url: "mapBasic.aspx/GetFinSys",dataType: 'json',data: function (term,page) {
            return "{'term':\"" + term + "\"}";
        },results: function (data,page) { // parse the results into the format expected by Select2.
            // since we are using custom formatting functions we do not need to alter remote JSON data
            return { results: data.Value };
        }
    }
});

ajax调用是在同一页面代码隐藏中的webmethod / pagemethod:

[WebMethod]
    public static List<LookupCodeItem> GetFinSys(string term)
    {
        string stringToCompareTo = term.ToLower();

        List<LookupCodeItem> result = new List<LookupCodeItem>();


        // FIN SYS
        using (mapEntities db = new mapEntities())
        {
            List<MPO_FINSYS_AMT> finSysCodes = (from x in db.MPO_FINSYS_AMT
                                                select x).ToList();

            foreach (MPO_FINSYS_AMT item in finSysCodes)
            {
                string valKey = string.Format("{0}.{1}.{2}",item.FY,item.MDOT_MPO_CD,item.FIN_SYS);
                LookupCodeItem x = new LookupCodeItem();
                x.Value = valKey;
                x.ShortDescription = string.Format("{0}.{1}.{2}",item.FIN_SYS); ;
                x.LongDescription = string.Empty;
                result.Add(x);

            }
        }

        return result;
    }

将数据输入到文本框中时,会发出POST请求,并且json发送似乎格式正确.

但是,pagemethod的响应是整个html页面.我的理解是,如果您没有在ajax调用中正确设置“contentType”,则可以通过post方法进行.我已经将它设置为与在页面上工作的所有其他ajax调用相同(它们不使用select2).

select2是否忽略“contentType”属性?还是还有别的东西我做错了?

**编辑**
发布后,我发现这个问题列在select2的github网站上:
Issue 492 – Add Support for contentType to Ajax

看来它没有通过contentType通过.我能绕过selet2的内置ajax帮助器,并使用我自己手动定义的一个吗?

我有同样的问题,下面的解决方案适用于我:
ajax: {
   ...
   params: { // extra parameters that will be passed to ajax
        contentType: "application/json; charset=utf-8",}
   ...
}
原文链接:https://www.f2er.com/ajax/159839.html

猜你在找的Ajax相关文章