weixin_33735676 2015-02-01 13:33 采纳率: 0%
浏览 196

Select2 AJAX无法正常工作

Here is the web method, which I use before for another thing, and it works:

[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:

$('#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.

  • 写回答

1条回答 默认 最新

  • weixin_33721427 2015-02-01 13:46
    关注

    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:

    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. This may be one cause, why your code does not work.

    评论

报告相同问题?

悬赏问题

  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大