我试图使用ajax函数来检索数据,该函数无法正常工作

时间:2017-11-20 11:51:24

标签: javascript c# asp.net ajax

在asp.net webform中我试图使用ajax调用来调用数据。我有其他页面的类似功能,工作正常,但在这个页面上我收到错误。

I am getting pageCountInt as NaN

<div id="loadmore" class="loadmore-w"><p id="loadmore-btn" >Load more</p></div>

表有大约6条记录,它加载前2条记录,当我查看加载更多按钮时,它不会加载任何更多数据并显示pageCountInt as NaN

var pageIndexInt = 0;
 var pageCountInt = 0;

 GetInterviewRecords();

 $('#loadmore-btn').click(function() {
     // console.log("button clicked");
     GetInterviewRecords();
 });

 function GetInterviewRecords() {
     //console.log("i am in GetInterviewRecords  function");
     pageIndexInt++;
     console.log("pageIndexInt " + pageIndexInt);
     console.log("pageCountInt " + pageCountInt);

     if (pageIndexInt == 1 || pageIndexInt <= pageCountInt) {
         $("#loading-interview").show();

         $.ajax({
             type: "POST",
             url: "<%= ResolveUrl ("~/en/Default.aspx/GetInterviews ") %>",
             data: '{pageIndex: ' + pageIndexInt + '}',
             contentType: "application/json; charset=utf-8",
             dataType: "json",
             success: OnSuccessInterview,
             failure: function(response) {
                 alert(response.d);
             },
             error: function(response) {
                 alert(response.d);
             }
         });
     }


 }

 function OnSuccessInterview(response) {
     // console.log("i am in OnSuccessInterview  function");
     var xmlDocInterview = $.parseXML(response.d);
     console.log("Interview XML  ----");
     //console.dirxml(xmlDocInterview);
     //console.dir(xmlDocInterview);
     var xmlInterview = $(xmlDocInterview);

     pageCountInt = parseInt(xmlInterview.find("pageCount").eq(0).find("pageCount").text());
     var interview = xmlInterview.find("Table");
     interview.each(function() {
         var articleItem = $(this);
         var aImagePath = '<%= ResolveUrl ("http://website.com/images/Interview/")%>' + articleItem.find("PageImage").text();
         var aTitle = articleItem.find("Heading").text();


         var aURL = 'http://website.com/interview/' + aID + "/" + aTitle;

         $('<div class="tab-item-wrapper"><a href="' + aURL + '" class="tab-i-a-a"><img src="' + aImagePath + '" class="tab-i-a-img"><span class="tab-i-a-title">' + aTitle + '</span></a></div>').appendTo(".wrapper");
     });

     if (pageIndexInt >= pageCountInt) {
         $("#loadmore").hide();
     }
 }

C#功能

 public static DataSet GetInterviewListByLangID(int LangID, int PageIndex, int PageSize)
    {
        DataSet ds = null; int PageCount = 0;
        try
        {
            SqlParameter[] sqlparam = new SqlParameter[4];
            sqlparam[0] = new SqlParameter("@LangID", LangID);
            sqlparam[1] = new SqlParameter("@PageIndex", PageIndex);
            sqlparam[2] = new SqlParameter("@PageSize", PageSize);
            sqlparam[3] = new SqlParameter("@PageCount", PageCount);
            sqlparam[3].Direction = ParameterDirection.Output;
            ds = SqlHelper.ExecuteDataset(connectionString, CommandType.StoredProcedure, "usp_GetInterviewListwise", sqlparam);

            DataTable dt = new DataTable("PageCount");
            dt.Columns.Add("PageCount");
            dt.Rows.Add();

            int TotalPageCount = 0;

            if (!String.IsNullOrEmpty(sqlparam[3].Value.ToString()))
            {
                TotalPageCount = (int)sqlparam[3].Value;
            }
            dt.Rows[0][0] = TotalPageCount;
            ds.Tables.Add(dt);
        }
        catch (Exception ex)
        {
            //HttpContext.Current.Response.Redirect("Message.aspx?msg=Invalid Request");
        }

        return ds;
    }

 [WebMethod]
    public static string GetInterviews(int pageIndex)
    {
        // System.Threading.Thread.Sleep(1000);
        return GetInterviewListByLangID(1, pageIndex, 2).GetXml();
    }

SQL SERVER SP

ALTER PROCEDURE [dbo].[usp_GetInterviewListwise]
      @LangID int
      ,@PageIndex INT = 1
      ,@PageSize INT = 6
      ,@PageCount INT OUTPUT
AS
BEGIN
      SET NOCOUNT ON;
      SELECT ROW_NUMBER() OVER
            (
                  ORDER BY [PublishDate] DESC
            )AS RowNumber
      ,[InterviewID] AS ID
      ,[Title] As Heading
      ,[Description] AS Brief
      ,REPLACE(CONVERT(CHAR(10), [PublishDate], 103), '/', '-') AS ReleaseDate
      ,[Image] AS PageImage
       ,[VideoID] AS VideoID

    INTO #Results
      FROM Interview WHERE Visible = 1 AND Active = 1 AND LanguageID = @LangID
      AND InterviewID NOT IN (SELECT TOP 1 InterviewID  FROM Interview WHERE LanguageID=@LangID AND Active=1 AND Visible = 1  Order by PublishDate DESC)

      DECLARE @RecordCount INT
      SELECT @RecordCount = COUNT(*) FROM #Results

      SET @PageCount = CEILING(CAST(@RecordCount AS DECIMAL(10, 2)) / CAST(@PageSize AS DECIMAL(10, 2)))
      PRINT       @PageCount

      SELECT * FROM #Results
      WHERE RowNumber BETWEEN(@PageIndex -1) * @PageSize + 1 AND(((@PageIndex -1) * @PageSize + 1) + @PageSize) - 1


      DROP TABLE #Results
END

我如何改进此代码并将其转换为json或修复与使用XML相同的代码。我不确定为什么pageCountInt值是NaN,我发现调试它也更难。

1 个答案:

答案 0 :(得分:0)

我解决了这个问题,因为我在以下代码留言中使用PageCount作为pageCount

pageCountInt = parseInt(xmlInterview.find("PageCount").eq(0).find("PageCount").text());

浪费了4个小时来弄清楚可能是什么问题。 CaSe SeNsiTiVe OHHHH !!!