如何使用php ajax进行file_get_contents和刷新数据而无需重新加载页面

时间:2018-10-24 08:21:09

标签: php ajax api file-get-contents reload

我正在使用php从API中获取一些json数据。我希望数据每20秒刷新一次,而无需手动重新加载页面。这是我下面的代码,非常适合于获取数据。只需要弄清楚如何刷新-也许使用Ajax?

    private void Form1_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("id");
        dt.Columns.Add("name");
        dt.Columns.Add("quantity");

        DataRow dr;

        dr = dt.NewRow();
        dr["id"] = "1";
        dr["name"] = "mobile";
        dr["quantity"] = "100";

        dt.Rows.Add(dr);

        dr = dt.NewRow();
        dr["id"] = "2";
        dr["name"] = "laptop";
        dr["quantity"] = "50";

        dt.Rows.Add(dr);

        this.dataGridView1.DataSource = dt;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < dataGridView1.Rows.Count; i++)
        {
            dataGridView1.ClearSelection();
            dataGridView1.Rows[i].Selected = true;

            if (dataGridView1.SelectedCells.Count > 0)
            {
                int selectedrowindex = dataGridView1.SelectedCells[i].RowIndex;

                DataGridViewRow selectedRow = dataGridView1.Rows[selectedrowindex];

                string _name = Convert.ToString(selectedRow.Cells["name"].Value);
                decimal _quantity = Convert.ToDecimal(selectedRow.Cells["quantity"].Value);
                MessageBox.Show("name: " + "quantity: " + _quantity.ToString());
                dataGridView1.Rows[i].Selected = false;
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

使用AJAX请求数据:

function requestMyData() {
    $.ajax({
        url: "/url/to/interface.php",
        type: get,
        success: function(data) {
            //update your UI here
        }
    });
}

然后您可以使用超时请求数据:

setTimeout(requestMyData(), 100);