如標題
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1_20210221計算停留時間POST後台.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<script src="Scripts/jquery-1.12.4.min.js"></script>
<script>
var second=0;
var minute=0;
var hour=0;
window.setTimeout("interval();", 1000); //每秒計算時間
window.setTimeout("PostStay();",5000); //五秒POST一次
function interval() {
second++;
if (second == 60) {
second = 0;
minute += 1;
}
if (minute == 60) {
minute = 0;
hour += 1;
}
var stayTxt = hour + "時" + minute + "分" + second + "秒";
$("#<%= Label1.ClientID%>").text(stayTxt);
$("#<%= HiddenField1.ClientID%>").val(stayTxt);
window.setTimeout("interval();", 1000);
}
//五秒鐘POST 一次
function PostStay() {
var stayTxt = $("#<%= HiddenField1.ClientID%>").val();
$.ajax({
url: 'WebForm1.aspx/GetStay',
method: 'post',
data: "{'val':'" + stayTxt + "'}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function () {
},
error: function (err) {
}
});
window.setTimeout("PostStay();", 5000);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:HiddenField ID="HiddenField1" runat="server" />
</form>
</body>
</html>
前端代碼
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static void GetStay(string val) {
///POST回來的資料 => val
if (!string.IsNullOrEmpty(val))
{
var AA = "";
}
}
}
後端代碼:
接收 POST回來的資料
2021/2/26 後記: 遇到要寫入資料庫問題,最後我還是乖乖用 post 到泛型處理常式(.ashx) 去接資料,存資料庫
以上文章僅用紀錄資料使用.....