【问题标题】:asp.net Polling from database with ajax使用ajax从数据库轮询asp.net
【发布时间】:2018-10-01 04:33:39
【问题描述】:

我已经查看了与此相关的所有其他问题,但似乎找不到适合我的问题的答案。

我有一个数据库,其中包含测量值和测量的相应日期时间。我目前在 asp.net Web 应用程序中使用 ajax 从我的数据库(sql server)中获取数据并用该数据制作图表。问题是我必须重新加载页面才能看到进入数据库的新值,这意味着只有加载页面时存在的数据库值在 Web 应用程序上可见。我想每隔一秒左右轮询一次新的数据库值,但在这种情况下我不知道该怎么做。

现在更新:我尝试使用 $.Ajax 没有任何成功。我现在正试图从 web 方法返回一个数组'y'(y 值)并从 ajax 调用中获取它。请参阅更新的代码。问题仍然在于 .aspx 中的 $.Ajax 方法。

最后更新:我按照 Josh Mein 的步骤弄清楚了。我不得不阅读文档等等,但我学到了很多东西。如果有人遇到类似的问题,我会在下面做一个比较:

.aspx(之前):

<head runat="server">
<title></title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="Scripts/jQuery-1.6.4.min.js"></script>
<script>
    setInterval(FillGraph, 1000);
function FillGraph() {
    $.ajax({
        type: "POST",
        url: "WebForm1.aspx/getArray",
        data: '{ }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: OnError
    });

    function OnSuccess(response) {
    //Fill the graph here i guess(?)
    //I tried alert(response.d) and it didnt return anything.
    }
</script>

.aspx(之后):

function makeChart() {
        $.ajax({
            type: "POST",
            url: "WebForm1.aspx/TestDouble",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                for (var i = 0; i < Object.keys(msg.d).length; i++) {
                    yVals[i] = msg.d[i];
                }
            }
        });

        $.ajax({
            type: "POST",
            url: "WebForm1.aspx/TestDate",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (msg) {
                for (var i = 0; i < Object.keys(msg.d).length; i++) {
                    xVals[i] = msg.d[i];
                }
            }
        });

        var trace1 = {
            x: xVals,
            y: yVals,
            fill: 'tozeroy',
            type: 'scatter'
        };
        var data = [trace1];

        Plotly.newPlot('myDiv', data);
    }

aspx.cs(之前):

public partial class WebForm1 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    [WebMethod]
    public static double[] getArray()
    {
        DataTable dt = new DataTable();
        using (SqlConnection con = new SqlConnection(@"Data Source =localhost\SQLEXPRESS; Initial Catalog = HIDDEN; Integrated Security=True"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("HIDDEN", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            da.Fill(dt);
            con.Close();
        }


        string[] x = new string[dt.Rows.Count];
        double[] y = new double[dt.Rows.Count];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            y[i] = Convert.ToDouble(dt.Rows[i][0]);
            x[i] = dt.Rows[i][1].ToString();
        }
        return y;
    }
}

aspx.cx(之后):

[WebMethod]
    public static string[] TestDate()
    {
        DataTable dt = new DataTable();
        using (SqlConnection con = new SqlConnection(@"Data Source =localhost\SQLEXPRESS; Initial Catalog = WeatherSystem; Integrated Security=True"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("HIDDEN", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            da.Fill(dt);
            con.Close();
        }
        string[] xArray = new string[dt.Rows.Count];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            xArray[i] = dt.Rows[i][0].ToString();
        }
        return xArray;
    }

    [WebMethod]
    public static double[] TestDouble()
    {
        DataTable dt = new DataTable();
        using (SqlConnection con = new SqlConnection(@"Data Source =localhost\SQLEXPRESS; Initial Catalog = HIDDEN; Integrated Security=True"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("HIDDEN", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);

            da.Fill(dt);
            con.Close();
        }
        double[] yArray = new double[dt.Rows.Count];
        //string[] xArray = new string[dt.Rows.Count];
        for (int i = 0; i < dt.Rows.Count; i++)
        {
            //xArray[i] = dt.Rows[i][0].ToString();
            yArray[i] = Convert.ToDouble(dt.Rows[i][0]);
        }
        return yArray;
    }

TestDouble() 返回一个双精度值数组。 TestDate() 将相应的 DateTimes 返回到这些双精度值(测量值)。 GetChart() 执行 2 个 ajax 调用,一个用于 Date 值,一个用于 Double 值。然后用这些值填充图表。

【问题讨论】:

  • 有很多关于 SO 的 ajax 示例应该让您朝着正确的方向前进。只需在后面的代码中创建一个 WebMethod 并通过$.ajax$.get$.post 调用它。当调用返回时,使用数据重新创建您的图表。就目前而言,您的代码甚至没有显示出解决问题的尝试。
  • 感谢您的回答。相信我,我已经尝试过 $.ajax 方法和 $.get 等等。问题在于这些方法中的内容,例如 url、datatype(json?) 等等。
  • 您是否在documentation 中查找过$.ajax? jQuerys 文档真的很棒。
  • 如果您尝试使用它,请使用显示您尝试的一些代码更新您的问题。你可能犯了一个简单的错误。

标签: c# asp.net ajax database polling


【解决方案1】:

我解决了,请参阅帖子的最新更新以获取信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多