在AJAX中从ASHX获取数据javascript
问题描述:
创建: 处理程序在数据库中检查是否有新的消息。在AJAX中从ASHX获取数据javascript
我到目前为止所做的: 使用AJAX的Javascript每三秒运行一次处理程序。
我无能为力:将结果从ASHX处理程序返回给AJAX javascript,以便在结果为true时执行回发。
这里是JavaScript的:
//handle message upload
window.setInterval(function() {
$.ajax({
url: "handlecb.ashx",
type: "POST",
success: function (result) {
toastr.options = {
"closeButton": false,
"debug": false,
"newestOnTop": true,
"progressBar": false,
"positionClass": "toast-bottom-left",
"preventDuplicates": true,
"onclick": null,
"showDuration": "1300",
"hideDuration": "1300",
"timeOut": "5300",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
}
toastr.info('Online.');
if (result < 0) {
__doPostBack("<%= senbut.ClientID %>", "");
toastr.options = {
"closeButton": false,
"debug": false,
"newestOnTop": true,
"progressBar": false,
"positionClass": "toast-bottom-left",
"preventDuplicates": true,
"onclick": null,
"showDuration": "1300",
"hideDuration": "1300",
"timeOut": "5300",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
}
toastr.success('New message recieved.');
}
},
error: function (err) {
toastr.options = {
"closeButton": false,
"debug": false,
"newestOnTop": true,
"progressBar": false,
"positionClass": "toast-bottom-left",
"preventDuplicates": true,
"onclick": null,
"showDuration": "1300",
"hideDuration": "1300",
"timeOut": "5300",
"extendedTimeOut": "1000",
"showEasing": "swing",
"hideEasing": "linear",
"showMethod": "fadeIn",
"hideMethod": "fadeOut"
}
toastr.danger('Offline');
}
});
}, 3000);
这里的处理程序:
<%@ WebHandler Language="VB" Class="handlecb" %>
Imports System.Data
Imports System.IO
Imports System.Data.OleDb
Imports System.Web.SessionState
Imports System.Web.Security
Imports System.Web
Public Class handlecb
Implements IHttpHandler, System.Web.Session
State.IRequiresSessionState
Dim con As OleDbConnection = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Ishaan Patel\Desktop\Paperhome\paperhome_data.accdb")
Dim cmd As OleDbCommand
Dim cmd2 As OleDbCommand
Dim da As OleDbDataAdapter
Dim ds As New DataSet
Dim dr As OleDbDataReader
Dim result As Integer
Public Sub ProcessRequest(ByVal context As HttpContext) Implements System.Web.IHttpHandler.ProcessRequest
Dim threadid As String
If Not context.Session("chatsess") Is Nothing Then
threadid = context.Session("chatsess")
GoTo b
Else
GoTo a
End If
b:
Dim thread As Integer
thread = Convert.ToInt32(threadid)
con.Open() 'checklastupdatedon
cmd = New OleDbCommand("SELECT [last_updated_on] FROM [message_threads] WHERE ([message_threads].[thread_id] = " & thread & ")", con)
cmd.Connection = con
dr = cmd.ExecuteReader
If (dr.Read) Then
Dim fetched As Date = Convert.ToDateTime(dr("last_updated_on"))
Dim old As Date = Convert.ToDateTime(context.Session("last_updated_on"))
result = DateTime.Compare(old, fetched)
con.Close()
If result < 0 Then 'old fetched is earlier than timer fetched
context.Session("last_updated_on") = fetched
End If
End If
a:
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return result
End Get
End Property
End Class
任何帮助/建议表示赞赏。谢谢。
答
调用处理程序时,函数名缺失。 请更改代码如下:
url: "handlecb.ashx/ProcessRequest",
type: "POST",
是否做到了这一点。它仍然不会通知收到新消息的时间。我试图按下按钮的逻辑,它工作正常,所以我相信处理程序不是问题。我返回的结果是正确的吗? –
请点击按钮后发布您收到的回复。 – user3151766
没有按钮点击发生或应该发生。所有这一切都应该没有任何按钮点击工作。虽然清除 - 结果