我正在尝试使用
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 GetFinSys(string term)
{
string stringToCompareTo = term.ToLower();
List result = new List();
// FIN SYS
using (mapEntities db = new mapEntities())
{
List 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”属性?还是还有别的东西我做错了?
看来它没有通过contentType通过.我能绕过selet2的内置ajax帮助器,并使用我自己手动定义的一个吗?