我尝试在服务器端使用Web服务(.asmx)进行分页,问题是响应。响应来自xml格式而不是json。
<?xml version="1.0" encoding="utf-8"?><string xmlns="http://tempuri.org/">{"Total":5,"Results":[{"id":5,"text":"a a a"},{"id":4,"text":"name yenice"},{"id":2,"text":"user user1"},{"id":1,"text":"user user2"},{"id":3,"text":"user3"}]}</string>
Js代码在这里:
$("#test").select2({
minimumInputLength: 2,
minimumResultsForSearch: 10,
ajax: {
url: URL,
dataType: "json",
type: "GET",
data: function (params) {
var c = {
searchTerm: '', // search term
pageNum: 1,
pageSize: 10
}
return c;
},
processResults: function (data) {
return {
results: $.map(data, function (item) {
return {
text: item.tag_value,
id: item.tag_id
}
})
};
}
}
});
答案 0 :(得分:1)
最后我解决了服务器端的问题:
[WebMethod(EnableSession = true)]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public void Sample(string searchTerm, int pageSize, int pageNum)
{
//var result....
Context.Response.Write(JsonConvert.SerializeObject(result));
}