使用ajax post方法的select2 [英] select2 with ajax post method

查看:116
本文介绍了使用ajax post方法的select2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 select2 进行Ajax加载.

I am trying to use select2 with ajax loading.

这是我的代码:

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上进行的:

The ajax call is to a webmethod/pagemethod in the code-behind of the same page:

[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.FY, item.MDOT_MPO_CD, item.FIN_SYS); ;
                x.LongDescription = string.Empty;
                result.Add(x);

            }
        }

        return result;
    }

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

When entering data into the textbox, the POST request is made, and the json is sends appears to be properly formatted.

但是,来自pagemethod的响应是整个html页面.据我了解,如果您没有在ajax调用中正确设置"contentType",则使用post方法可能会发生这种情况.我已将其设置为与在页面上正常工作的所有其他ajax调用相同(它们未使用select2).

However, the response from the pagemethod is the entire html page. It is my understanding that this can occur with post methods if you do not have your "contentType" properly set in the ajax call. I have set it that same as all my other ajax calls that do work on the page (they are not using select2).

select2是否忽略"contentType"属性?还是我做错了其他事情?

Does select2 ignore the "contentType" attribute? Or is there something else I've done incorrectly?

**编辑** 发布此内容后,我发现此问题列在select2的github站点上: 问题492-将对contentType的支持添加到Ajax

** EDIT ** After posting this, I found this issue listed at select2's github site: Issue 492 - Add Support for contentType to Ajax

似乎没有通过contentType.我可以绕过selet2内置的ajax帮助程序,而使用我自己的手动定义的帮助程序吗?

It appears that it doesn't pass contentType through. Am I able to bypass selet2's built in ajax helper and use my own manually defined one?

推荐答案

我遇到了同样的问题,以下解决方案对我有效:

I was have same problem and below solution works for me:

ajax: {
   ...
   params: { // extra parameters that will be passed to ajax
        contentType: "application/json; charset=utf-8",
   }
   ...
}

这篇关于使用ajax post方法的select2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆