简体   繁体   English

我正在尝试使用作为无法正常工作的ajax函数来检索数据

[英]I am trying to retrive data using as an ajax function which is not working properly

In asp.net webform i am trying to call data using ajax call. 在asp.net Webform中,我尝试使用ajax调用来调用数据。 I have similar function of other page which works fine but on this page i am getting error. 我在其他页面上也有类似的功能,但工作正常,但是在此页面上我遇到了错误。

I am getting pageCountInt as NaN

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

Table has about 6 records and it loads the first 2 records and when i cluck on the load more button it doesnt load any more data and show pageCountInt as NaN 表大约有6条记录,它加载了前2条记录,当我按了“加载更多”按钮时,它不再加载任何数据,并且将pageCountInt as NaN显示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# Function 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 SQL服务器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

How i can improve this code and convert this into json or fix the same where i work with XML. 我如何改进此代码并将其转换为json或在使用XML的地方进行修复。 I am not sure why pageCountInt value is NaN and i am finding it even more hard to debug it also. 我不确定为什么pageCountInt的值为NaN,而且我发现调试它也更加困难。

I solved the issue as i was using PageCount as pageCount in the following lien of code 我在以下留置权代码pageCount PageCount用作pageCount解决了该问题

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

Just wasted 4 hours figuring out what could be the issue. 只是浪费了4个小时才弄清楚可能是什么问题。 CaSe SeNsiTiVe OHHHH!!! 小心!呵呵!!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM