[英]Select2 AJAX is not working
Here is the web method, which I use before for another thing, and it works: 这是Web方法,我之前将其用于另一件事,并且可以正常工作:
[WebMethod]
public static string GetTests()
{
return GetData().GetXml();
}
public static DataSet GetData()
{
DataSet ds = new DataSet();
BusinessLayer.StudentsController oStudent = new BusinessLayer.StudentsController();
oStudent.Action = "page";
oStudent.PageIndex = 0;
oStudent.PageSize = int.Parse(System.Configuration.ConfigurationManager.AppSettings["PageSize"].ToString());
DataTable dt1 = oStudent.Select();
DataTable dt2 = dt1.Copy();
dt2.TableName = "Students";
ds.Tables.Add(dt2);
DataTable dt = new DataTable("PageCount");
dt.Columns.Add("PageCount");
dt.Rows.Add();
dt.Rows[0][0] = oStudent.PageCount;
ds.Tables.Add(dt);
return ds;
}
JavaScript: JavaScript的:
$('#selectDynamic1').select2({
placeholder: "Search for a movie",
minimumInputLength: 1,
multiple: true,
ajax: {
url: "Default.aspx/GetTests",
type: 'POST',
params: {
contentType: 'application/json; charset=utf-8'
},
dataType: 'json',
data: function (term, page) {
return JSON.stringify({ q: term, page_limit: 10 });
},
results: function (data) {
return {results: data};
},
},
formatResult: formatResult,
formatSelection: formatSelection,
/*initSelection: function(element, callback) {
var data = [];
$(element.val().split(",")).each(function(i) {
var item = this.split(':');
data.push({
id: item[0],
title: item[1]
});
});
//$(element).val('');
callback(data);
}*/
});
function formatResult(node) {
alert('');
return '<div>' + node.id + '</div>';
};
function formatSelection(node) {
alert('');
return node.id;
};
Please help, GetTests
is not even firing, I want to bring the student through SQL then fill the select2. 请帮助, GetTests
甚至没有解雇,我想让学生通过SQL,然后填写select2。
First, you shoul ry to change the HTTP verb to GET
, as you are not posting data to the server, you want to get data from it: 首先,您应该将HTTP动词更改为GET
,因为您没有将数据发布到服务器,而是要从中获取数据 :
ajax: {
...
type: 'GET',
...
}
Secondly, you are expecting JSON from the server, but in the GetData
method, you are creating an XML document - at least the code conveys this. 其次,您期望服务器提供JSON,但是在GetData
方法中,您正在创建XML文档-至少代码可以传达这一点。 This may be one cause, why your code does not work. 这可能是导致您的代码无法正常工作的原因之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.