简体   繁体   English

在ASP.NET中的JQuery函数遇到问题吗?

[英]Having problems with JQuery function in asp.net?

I have written a jQuery function which calls ac# method. 我已经编写了一个调用ac#方法的jQuery函数。

If the function returns a success then it calls another method from the c# code which is responsible for incrementing the counter variable in my c# class. 如果函数返回成功,则它将从c#代码中调用另一个方法,该方法负责增加c#类中的计数器变量。

I wanted to do automatic counter increment every 1 minute and this is what I noticed. 我想每1分钟执行一次自动计数器递增,这就是我注意到的。 I set up a breakpoint in my Counter() function in my c# class. 我在c#类的Counter()函数中设置了一个断点。 As the page loads up and the Counter method is called, I proceed with my debugging and notice that things work fine but once the counter variable reaches the value "2", when I press F10 to step into my Counter method(), it doesn't reach the end of the method and increments the counter variable by 2 and then from here on things get worse. 当页面加载完毕并调用Counter方法时,我继续进行调试,并注意到一切正常,但是一旦counter变量达到值“ 2”,当我按F10进入我的Counter method()时,它不会不会到达方法的末尾,将计数器变量加2,然后从这里开始,情况会变得更糟。

I wonder what could I be doing wrong? 我想知道我会做错什么吗? Can anyone review my script and give me suggestions on what could be causing the error? 任何人都可以查看我的脚本并给我一些可能导致错误的建议吗?

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Dtata.aspx.cs" Inherits="Dtata" %>
  <!DOCTYPE html>

  <html xmlns="http://www.w3.org/1999/xhtml">

  <head runat="server">
    <title></title>
    <script src="/scripts/jquery-3.1.1.min.js"></script>

  </head>

  <body>
    <form id="form1" runat="server">
      <div>
      </div>
      <script type="text/javascript">
        klm();

        function klm() {
          $.ajax({
            type: "POST",
            url: "Dtata.aspx/Hello",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "{ 'name' : 'hello' }",
            success: function(result) {
              console.log(result.d);
              Counter() //<-- CALL OTHER AJAX METHOD TO INCREASE COUNTER ON BACK END
            },
            error: function(result) {
              alert(result.responseText);
            }
          });
        }


        function Counter() {
          $.ajax({
            type: "POST",
            url: "Dtata.aspx/Counter",
            contentType: "application/json; charset=utf-8",
            dataType: "json",

            success: function(result) {
              console.log(result.d);
            },
            error: function(result) {
              alert(result.responseText);
            }
          });
          setInterval(Counter, 60000);


        }
      </script>



    </form>


  </body>
  </html
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Dtata: System.Web.UI.Page {
  public static int counter = 0;

  protected void Page_Load(object sender, EventArgs e) {


  }

  [WebMethod]
  public static string Hello(string name) {
    return name;
  }

  [WebMethod]
  public static int Counter() {
    counter = counter + 1;
    Console.WriteLine("I have been called" + counter);

    return counter;
  }
}

One thing. 一样东西。 You call counter in klm and also in setInterval. 您可以在klm中以及在setInterval中调用counter。

If you want to loop an AJAX function, instead do this: 如果要循环使用AJAX函数,请执行以下操作:

function Counter() {
  $.ajax({
    type: "POST",
    url: "Dtata.aspx/Counter",
    contentType: "application/json; charset=utf-8",
    dataType: "json",

    success: function(result) {
      console.log(result.d);
      setTimeout(Counter,60000); // call again if ok - or in .done
    },
    error: function(result) {
      alert(result.responseText);
    }
  });
}

Another thing - I would expect your C# to read and write the counter to a DB and I do not see the C# return any JSON with a { d:counter} 另一件事-我希望您的C#可以将计数器读写到数据库,并且我看不到C#会返回带有{ d:counter} JSON

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

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