Web方法未在FlexiGrid中触发

Webmethod not firing in FlexiGrid

本文关键字:FlexiGrid 方法 Web      更新时间:2023-09-26

我正在为我的项目使用 FlexiGid。但问题是Web方法没有触发。(Json/Ajax call(我已经在Web方法上放置了一个调试点,但它没有触发,Firebug也显示Web方法Url是正确的。

在这里我把代码放了

阿贾克斯呼叫

  function flexgrid() {
        debugger;
        $("#flex1").flexigrid({
                    url: '/WebMethods.aspx/GetIssueSummaryById',
                    dataType: 'json',
                    contentType: "application/json; charset=utf-8",
                    colModel : [
                        {display: 'ID', name : 'id', width : 40, sortable : true, align: 'center'},

                    ],
                    data: JSON.stringify({ ProjectId: "1", UserId: "1" }), //Hard code this values at this time
                    buttons : [
                        { name: 'Add', bclass: 'add', onpress: test },
                        { name: 'Delete', bclass: 'delete', onpress: test },
                        {separator: true},
                        {name: 'A', onpress: sortAlpha},
                        {name: 'B', onpress: sortAlpha}

                    ],
                    searchitems : [
                        { display: 'Project', name: 'project' },
                        {display: 'Name', name : 'name', isdefault: true}
                    ],
                    sortname: "id",
                    sortorder: "asc",
                    usepager: true,
                    title: 'Issue Summary',
                    useRp: true,
                    rp: 10,
                    showTableToggleBtn: true,
                    width: 1000,
                    height: 500
                });
    };
Web

Method(在 WebMethods.aspx 文件中(

 [WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static List<IssuesVM> GetIssueSummaryById(string UserId, string ProjectId)
{
    //Guid LoggedInUserId = new Guid(UserId);
    //int ProjectId = Convert.ToInt32(ProjectId);
    List<IssuesVM> lst = new List<IssuesVM>();
    try
    {
        SqlCommand comIssueSummary = new SqlCommand("SP_GetIssuesByProjectIDAndOwnerId", conn);
        comIssueSummary.CommandType = CommandType.StoredProcedure;
        //comIssueSummary.Parameters.Add("@ProjectId", SqlDbType.Int).Value = ProjectId;
       // comIssueSummary.Parameters.Add("@UserId", SqlDbType.UniqueIdentifier).Value = LoggedInUserId;
        if (conn.State == ConnectionState.Closed)
            conn.Open();
        SqlDataReader rdr = comIssueSummary.ExecuteReader();
        DataTable dt = new DataTable();
        dt.Load(rdr);
        foreach (DataRow r in dt.Rows)
        {
           //Some code goes here
        }
    }
    catch (Exception)
    {
        throw;
    }
    return lst;
}

之后,Firebug显示了这一点图片在这里

谁能知道这个错误?不发射网络方法?

P.S - 我在下面的帖子[点击这里]中看到了一些解决方案,我对 flexigrid.js 文件做了那个,但它也不起作用。

这是变化FlexiGrid.js 檔案 (更改前(

    $.ajax({
                type: p.method,
                url: p.url,
                data: param,
                dataType: p.dataType,
                success: function (data) {
                    g.addData(data);
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    try {
                        if (p.onError) p.onError(XMLHttpRequest, textStatus, errorThrown);
                    } catch (e) {}
                }
            });
        },

灵活网格.js (变更后(

 $.ajax({
                    contentType: "application/json; charset=utf-8",
  data: "{}", // to pass the parameters to WebMethod see below 
                    success: function (data) {
                        g.addData(data);
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        try {
                            if (p.onError) p.onError(XMLHttpRequest, textStatus, errorThrown);
                        } catch (e) {}
                    }
                });
            },

因此,首先,将其移动到WebService.asmx文件中可能是个好主意。这样做是最好和常见的做法。.ASPX页面通常使用HTML/CSS/Javascript响应,.asmx使用JSON或XML响应。

无论

哪种方式,无论 Ajax 对 flexigrid 的调用是针对 Web 服务还是 Web 窗体页面,当您添加属性 [WebMethod] 以公开执行第一个 Ajax 调用的方法时,都会有点挑战性。关于Ajax对公共WebMethods的调用,有一些挑剔的东西。挑剔出现在请求的内容类型以及请求是 JSON 还是 XML 以及响应是 JSON 还是 XML。

因此,我将向您展示我所知道的适用于我使用Flexigrid的项目的内容:

 $('#gridTablegSearchProperty').flexigrid({
        url: 'Services/WSgSearch.asmx/gridTablegSearchProperty',
        colModel: [...

您会注意到,在第一个代码片段中,我没有设置Flexigrid的contentType或dataType属性。

现在我的网络方法签名

 [WebMethod]
    public XmlDocument gridTablegSearchProperty()
    {
        System.Collections.Specialized.NameValueCollection nvc = HttpContext.Current.Request.Form;
        int pgNum = nvc.GetValueAsInteger("page").GetValueOrDefault(1);
        int pgSize = nvc.GetValueAsInteger("rp").GetValueOrDefault(20);
        string sortName = nvc.GetValueOrDefaultAsString("sortname", "key");
        string sortOrder = nvc.GetValueOrDefaultAsString("sortorder", "desc");
        string query = nvc.GetValueOrDefaultAsString("query", string.Empty);
        string qtype = nvc.GetValueOrDefaultAsString("qtype", string.Empty);

我的 WebMethod 在 .asmx 文件中,如果您将自己的 WebMethod 保存在代码隐藏文件中并不重要,但我会移动到 Web 服务并删除 WebMethods.aspx这是糟糕的命名约定和文件使用约定。