javascript在点击链接时触发并忘记-异步ajax
我们正在数据库中存储事件。一个事件可能是页面点击和保存/更新等。从控制器内部的操作,这是发生得很好 我有3个链接,当点击这些链接时,我想在数据库中存储一个事件。我希望在幕后通过jquery/ajax来实现这一点,而不是让用户执行一般操作来存储事件,然后执行response.redirect。现在,当用户单击链接时,应该发生两件事javascript在点击链接时触发并忘记-异步ajax,javascript,jquery,ajax,asp.net-mvc,Javascript,Jquery,Ajax,Asp.net Mvc,我们正在数据库中存储事件。一个事件可能是页面点击和保存/更新等。从控制器内部的操作,这是发生得很好 我有3个链接,当点击这些链接时,我想在数据库中存储一个事件。我希望在幕后通过jquery/ajax来实现这一点,而不是让用户执行一般操作来存储事件,然后执行response.redirect。现在,当用户单击链接时,应该发生两件事 ajax请求将创建一个事件 用户被带到预期页面 我只想让用户点击链接,用户就可以进入预期的页面当代码等待远程操作的响应时,后台处理不应让用户在页面上再停留一两秒钟。我需
$(document).ready(function() {
$("a[custom-event]").on('click', function () {
var label = $(this).attr("custom-event");
var value = $(this).attr("custom-event-value");
DoSomeAjaxProcessing('event', 'click', label, value); //don't want this to go bad if redirection happens immediately
return true; //return true so that the browser can send the user to the href location
});
});
更新1:
像这样的东西会为我飞吗。我也将进行测试和更新
$(document).ready(function() {
$("a[custom-event]").on('click', function () {
var label = $(this).attr("custom-event");
var value = $(this).attr("custom-event-value");
(new Image).src = someurl+"?label="+label+"&value="+value+"&type=event&action=click";
return true; //return true so that the browser can send the user to the href location
});
});
如果您试图使用服务器记录事件,您将不得不承担一些成本。您不能异步调用服务器来记录事件并同时为该链接的目标提供服务 正如您所提议的,您所提议的两条道路并不令人满意:
/home
路由带到/login
路由,我们将在为/login
视图提供服务的控制器的开头跟踪该事件
[Route("/login")]
public virtual ActionResult Login()
{
// Tracking code here, probably passing in the referrer
return View();
}
你需要取消点击,然后在回调完成后执行操作。嗯,这是我试图避免的,我不想取消点击。谷歌分析如何处理点击跟踪?在这里,单击跟踪和目的地同时提供服务。我们所要做的就是点击setup
ga('send','event','button','click',label,value)
。我在这里选择了2。