Reputation: 942
Can someone please check why my .ashx file is not firing the alert is being hit fine
function setHeartbeat() {
setInterval("heartbeat()", 20000);
}
function heartbeat() {
alert('ajax called');
$.ajax({
type: "GET",
url: "SessionHeartbeat.ashx",
data: null,
success: function (data) {
beatHeart(30);
setHeartbeat();
}});
}
.ashx file looks like this and is called SessionHeartbeat.ashx
public class SessionHeartbeat : IHttpHandler, IRequiresSessionState
{
public bool IsReusable { get { return false; } }
public void ProcessRequest(HttpContext context)
{
context.Session["Heartbeat"] = DateTime.Now;
}
}
}
this is my web.config
<httpHandlers>
<add verb="GET,HEAD" path="SessionHeartbeat.ashx" validate="false" type="SessionHeartbeat"/>
</httpHandlers>
Upvotes: 0
Views: 4874
Reputation: 6683
The example you mentioned worked after updating web.config:
<configuration>
<!--Other settings-->
<system.webServer>
<validation validateIntegratedModeConfiguration="false"/>
</system.webServer>
<system.web>
<httpHandlers>
<add verb="GET,HEAD" path="SessionHeartbeat.ashx" validate="false" type="SessionHeartbeat"/>
</httpHandlers>
</system.web>
</configuration>
But this depends on IIS version and how it's configured. See this answer: Generic Handler not working on IIS. It explains that a Generic Handler still needs settings in your web.config in some scenarios and that was the case when I've followed the example in the link you posted.
Also I have previously added response write in ProcessRequest
public void ProcessRequest(HttpContext context)
{
context.Response.Write("Hello World");
context.Session["Heartbeat"] = DateTime.Now;
}
If you deploy this in IIS you probably will need to register your handler but it was not necessary in my IIS Express.
Upvotes: 1
Reputation: 62290
If you use handler class, you have to add them to <httpHandlers>
tag inside web.config.
What you have is a generic handler. You do not need to do anything, so remove it from <httpHandlers>
tag inside web.config.
The following code should work as long as you can access SessionHeartbeat.ashx
from client browser.
<script>
setInterval(function() {
$.ajax({
type: "GET",
url: "<%= ResolveUrl("~/SessionHeartbeat.ashx") %>",
data: null,
success: function (data) {
console.log('Heartbeat called at ' + data);
}
});
}, 20000);
</script>
public class SessionHeartbeat : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
var dateTimeNow = DateTime.Now;
context.Session["Heartbeat"] = dateTimeNow;
context.Response.Write(dateTimeNow);
}
public bool IsReusable { get { return false; } }
}
Upvotes: 3